| PHP Basics | |||||||
| |
Feature:
PHP 101 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.
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: 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: As you might guess, comment lines in a PHP file are just ignored by the PHP
interpreter. So the next line is: 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: 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. 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. | ||||||
Top of Page Home Tips & Tutorials © Imagination 2001-2004 |