PHP Basics

Feature: PHP 101
Credit: Imagenation

The basics of PHP are rather simple as shown in the diagram below. PHP is server-based just like ASP and JSP. So the webpage that is delivered to the user contains no PHP commands and directives, unlike in the case of JavaScript or the new ASP.NET, where scripting gets delivered along with the HTML webpage to the PC or other web device (PDA, mobile phone etc). This is what it means to be server-based. PHP programs pick up all the speed, power and database resources from the server; but at the expense of having only HTML/Java/JavaScript /CSS/DOM access at the client.

So it is clumsy if not down right hard to do form validations or interactive programs like a calculator in PHP. But for a very broad set of web applications, including form-driven web database updates, PHP works just fine and in fact has captured the lions share of Weblog, photo album, web portal, shopping cart, content management, e-business, and other applications for small to medium size businesses on the Web. In fact, PHP is now being used by major corporates from AT&T to Zion Bancorporation.

In an upcoming tutorial we will describe all the steps required to install PHP on Windows and Linux PCs. In the meantime, the following code is the classic and very basic Hello World program done in PHP. It gives a good intro into the basics of how the language works.
Now the code you see to the right resides on the web server. The user on the PC never sees this code. Rather the HTTP web server detects the <?php ... ?> directives and passes this code onto the PHP interpreter. Depending on the contents of the directives code, the PHP interpreter links to databases, other code, and/or other services and then generates the final HTML code which is transmitted to the requesting web browser and displayed there. So PHP can be thought of as a program generator using basic HTML templates as the skeleton.

Here is the details on how the process works. The first three lines are just standard HTML and the PHP interpreter just passes them through unchanged. The next line is the PHP tag signalled by the <?php opening marker. This signals to the PHP interpreter that its code follows until the closing ?> marker is found.

The first line of code is actually a comment, //this is a comment in PHP, yes just like in Java. The first line of excutable code is:
  echo "<h1>Hello World, ".$_ENV["USERNAME"];
The echo PHP command cause a line of code to be added to the developing web page. The first part of that line is the string, "<h1>Hello World, " - which all HTML coders will recognize as a h1 header tag followed by text. The novel part of this is the second part. First the period, "." acts a concatenation operator in PHP. It tells PHP to add on to the end of "<h1> Hello World, " the value of the $_ENV["USERNAME"] variable. PHP comes with several hundred predefined variables and functions. Most of the predefined variables are associated with web user, the web server and the parameters of the session they are conducting. One of those predefined variable is $_ENV["USERNAME"] - which is the username of the PC client.

Actually, this is not just a variable, but an array variable, hence the brackets, [...]. But it is not an ordinary array; but rather an associative array - hence the string, "USERNAME", is used as an index into the array, $_ENV. Here is another PHP rule - all variables in PHP begin with a dollar sign, $. It is a simple rule and makes variables in PHP easy to spot. Finally, the semicolon, ; indicates the end of the first executable line of PHP code. So after all is said and done PHP, appends the following line to the evolving web page:
<h1> Hello World, JBSurveyer

As you might guess, comment lines in a PHP file are just ignored by the PHP interpreter. So the next line is:
echo "</h1><h3>".date("l F jS, Y")." will be a good day at ".$_ENV["USERDOMAIN"];
The first substring, "</h1><h3>", just closes off the <h1> tag from the first line and starts a new <h3> tag which makes the font size smaller but it remains bold. However, after the dot concatenation operator, the next substring or token - date("l F jS, Y"), is an example of a predefined PHP function. date() returns the current date and time. The parameter string, "l F jS, Y", formats the current date as follows for the date of writing this tutorial => Thursday January 16th, 2003.

The next substring in the echo command line is " will be a good day at " and of course this is just appended onto the the second line. The final token or substring is $_ENV["USERDOMAIN"] which is just another predefined assciative array variable which returns the name of the PC client domain. So the second and final PHP output line is:
</h1><h3>Thursday January 16th, 2003 will be a good day at IMAGENAT-9O750Q
The last few lines of the file outside of the closing "?>" directive are also just appended to the output stream being generated by the PHP interpreter. The Figure to the left shows the final and complete web page after it has been through the PHP interpreter and ready for delivery to the PC web browser.

Note there are no 'leftover' PHP directives in the generated HTML code. This in fact is what makes PHP code hard to debug. All the work is done up at the server - not on the PC cient where the PHP code is being written. Some new PHP IDEs do allow for debugging on the server; but a better workaround is to have the web server on your PC. With Linux and Windows 2000 this is easy to do with Apache (or IIS in addition on Windows). Also in this sample we did not use any CSS, JavaScript, DOM, or other advanced HTML programming. But we easily could have as there are no restrictions on what can be generated by PHP.
So what does the final code look like in the web browser ? Well the figure to the left shows our Hello World in PHP output in the new, super powerful Mozilla browser. Trust me it looks identically the same in the Internet Explorer browser. And why should it not - its just HTML code.

Well some web veterans may have flashed a wide grin at that remark. Browser wars unfortunately continue. But with PHP's host of predefined variables it is possible to detect which browser a client is using and then direct the appropriate output to the browser. I know who wants to clean up after the browser vendors ? Well, if thats the case, then just switch your users to the browsers that most closely conform to the current Web standards while being fast, functional, reliable and secure. As of the current date that browser is - surprise, surprise - Mozilla/Netscape 1.2. Konqueror, Opera, Safari are distinctly behind; particularly in functionality and bringing up the rear is Internet Explorer. Quite a reversal of fortunes.

So in summary, we have seen that PHP is an interpreter, working up at the server and transforming HTML templates with embedded PHP directives into final, complete HTML pages for use on a Web client device (it does not have to be a PC). as we shall see in more detail through the tutorials, PHP strongly resembles Perl and Java in syntax and JSP or ASP in how it works between client and server.

Top of Page  Home   Tips & Tutorials
© Imagination 2001-2004