I am not able to figure out what we really mean when we echo javascript.
Does it mean; the function inside script will execute inevitably?
I mean, will it execute with out even being called?
I’ve seen long scripts that seem to include a complete page inside an echo statement.
echo "<script type='text/javascript'>
$(function(){
blah blah blah
});
</script>";
echois how you print output in PHP, it doesn’t do anything special otherwise.You can often see examples like yours in poorly organized, messy code. Like you say:
People get into a bad habit of “echoing” everything, rather than, in your case, using an external javascript file, or learning to break into or out of a PHP block for view logic.
Usually should be preferred to:
In general, you should avoid mixing HTML, CSS, and JavaScript into your PHP code unless it’s in a template or view file. Just remember there are a lot of inexperienced coders out there and you shouldn’t imitate everything you see.
It’s better to inject PHP into your HTML, not the other way around.