PHP is a web scripting language (akin to Microsoft’s Active Server Pages or Adobe’s ColdFusion.) Originally, it began as a humble collection of Perl scripts designed to ease the creation of popular web addons like guest books and message boards, PHP meaning simply “Personal Home Page.”
PHP is now in its 5th version and has received massive reworking and updates since its unpretentious beginnings, becoming a solid platform for building web sites at all levels. It provides basic interactive features through to dynamic enterprise applications. PHP is the most obvious starting point for learning how to work with the LAMP environment because it is the most visible component. PHP pages make up the user interface that people will interact with (whereas Linux and Apache provide the underlying engine, and MySQL gives database storage.)
Even if you do not actually run a Linux environment yourself, chances are your web host gives you PHP (and MySQL) for free. This is true of paid web hosts as well as any complimentary free web space that your ISP gives.
In fact, it’s well worth getting familiar with the basics of PHP just to enhance your own personal web page. It’s also worthwhile so you can debug or at least make sense of PHP pages you encounter. You’ll find it dead easy even if you’ve never programmed before.
Entering PHP mode
PHP code is interspersed with standard HTML tags. The web server – Apache, mostly, but PHP pre-processors can be added to IIS and other servers – receives a request from a web browser as usual. It picks up the PHP page and executes the programming commands inside, rendering any generated output along with the HTML tags. This final output is returned to the web browser, giving the illusion of an ordinary, static web page despite the fact it was generated right on the fly by the server.
The pre-processor fires when the web page suffix is .php instead of the regular .html (or .htm). It then looks inside the file for special tags which indicate where literal HTML output stops and PHP code begins. There are four possible tags that can be used, all achieving the same purpose:
When the pre-processor encounters one of these tags, it stops recognising text as HTML to be returned as-is and instead sees it as program code to execute. This continues until a closing tag is found, at which point the pre-processor just emits subsequent text as straight HTML again.
The closing tag must match the opening tag; use ?> for either of the first two options above, %> to close <% and </script> to match <script language=”php”>.
A variable is a data item that can have a value assigned to it. Over the course of the program you may alter this value and use it for display or in calculations.
Some languages are strict about how variables are used. If you specify it has integer values only you can’t then store a decimal number or a text string in it. PHP is different, and this is one of the reasons it is generally considered a fast language to work in. In PHP, a variable begins with a dollar sign ($) and that’s it; you do not explicitly state any restriction on the data it can hold. Instead, PHP will work out the data stored by how it is used and what’s more, it’ll go out of its way to help you work with different types of data.
Let’s put these together to make a real working PHP page.