From Template Engine to Language
How PHP's heritage shapes its use.
Audience: Early Intermediate. This post presumes you understand basic syntax, control structures and the including and requiring of files.
PHP is a curious language. In what other programming language do you have to enclose all the code in tags? The answer is none - because PHP began as a template engine - specially a quick template run time for C++. Somewhere between the original PHP and PHP 3 it picked up so much functionality that people where able to start writing sites with it without touching C, and by version 4 the language picked up its first implementation of classes and began picking up the true trappings of a language.
And yet PHP still does some things like a template engine - and it's important to understand this in order to avoid some weird bugs once you start trying to do more sophisticated things, particularly use the header function.
PHP works file by file. It compiles the first file handed to it by the webserver, then does this process again whenever you call include or require (and the first time you call include_once or require_once for a given file).
Because of this certain errors - parse errors mostly - will occur before anything in the file gets done. Also PHP mercifully stops at the first parse error it encounters - Most C compilers for example do not and will happily report all other parse errors in the file - the problem with this is that the first error often is the cause for those that follows
Provided the code parses (the term compiles some times gets used but this implies a permanence PHP doesn't have) the interpreter will start following instructions.
Here's one of the first gotcha's of PHP - anything and I do mean ANYTHING in the file outside the <?php ?> tags gets sent to output as soon the interpreter sees it. The problem with this is some functions - header most prominent among them - cannot be called after this occurs and will error out.
Avoiding this is simple enough - make sure the file starts on <?php before anything else. You don't have to have a closing ?> at the end of the file either if you don't - but be consistent whichever approach you want to do.
Another piece of PHP's heritage as a template engine is the presence of short tags. <? ?> and <?= ?> the "<?=" tag is equivalent to "<?php echo ". A third piece is PHP's braceless syntax.
Ok, that's all for now. Anyone got any questions or ideas for topics in this basics thread? One I will touch on later is the basics of errors.