Day Two: Generating HTML With PHP
As we saw in day one (Power HTML) HTML is a simple mark up language for documents with hyperlinks. With simple HTML, you can create wonderful, informative web pages. Unfortunately, the contents of such simple pages are set in stone. There is no way to customize the contents of the page for the user. If you want to get to the next level and make dynamic web pages, you will need a stronger tool...you will need a tool that writes HTML
PHP is an easy to learn server side scripting language. It is a program for generating HTML. In its original release, the acronym PHP stood for Personal Home Page. That's right, the base code for the language was pounded out by a web page author who wanted to add interactive features to his personal web site. (Today's programmer pretend the name stands for "PHP: Hypertext Preprocessor." Notice the use of a acronym in an acronynm.)
The open source movement took a liking to PHP. Computer geeks around the world added enhancements to PHP, and now PHP has become a major platform for web development.
Since PHP was written by web designers for web designers. It evolved into a language that is both easy to learn and is optimized for the web page creation.
How PHP Works
A web server is a rather simple beast. Its primary mission is to read files from the disk and toss the contents of the file into cyberspace.
For static web pages, this process is simply a matter of reading from the disk and transmitting the file through the Internet. PHP sits between the web server and its hard disk. On a PHP enabled web site, the server reads the file from the server. Executes any PHP commands on the page, then sends the resulting HTML to the client's browser.
PHP script uses the delimiter "<?php" to start the script and "?>" to end the script. The script <?php echo "<b>Hello World</b>"; ?> produces the HTML code <b>Hello World</b>.
This simple combination of PHP and HTML allows you to add dynamic elements to pre-existing web pages. The fact that PHP integrates easily with your existing pages is one of the primary selling points for the language.
One of the most useful features of PHP is the server side include. Rather than writing new PHP code for each page, you can save all of your PHP in a separate file and include it in multiple files. For example, I could put the navigation bar for my site in a file called myphp.php. I could then display the navigation bar on a page with the PHP command <?php include("myphp.php); ?>
Server side includes are extremely useful for things like menus, and formatting elements, logos, etc.. Once you are using server side includes, you can make make global changes by changing a single file.
PHP Enhancements v. Full Web Page Design
Most beginning PHP programmers begin by adding a few enhancements to their programs. Others, myself included, prefer to write the entire page in PHP. I will gear the conversation largely toward building entire PHP driven pages. With such pages, the first list of the code is <?php and the last line of the code is ?> to close the page.
Unfortunately, PHP is not as easy as HTML. It is a full programming language. To master PHP you will need to go through the painful process of learning to program. I suspect that most readers have a basic understanding of if statements and while loops. Not wishing to leave anyone out, I will take advantage of the multidimensional nature of hypertext so you can chose what to read next: