PHP

 <  Day Day Up  >  


PHP (www.php.net) is a very powerful and easy-to-use open source server-side scripting environment. While initially developed for the Linux/UNIX community, PHP can also be found on Windows server platforms. The reason for the widespread adoption of PHP is the simplicity of the language. For example, consider the simplest "hello world" example here:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <head>   <title>  PHP Hello World  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   </head>   <body>   <?php  print("Hello world from PHP!");  ?>   </body>   </html>  

In this example, the PHP code is delimited by <?php and ?> . The code simply outputs the text "Hello world from PHP!". If the file was saved as hello.php and uploaded to a PHP aware server, it would execute when requested , resulting in a seemingly static HTML page. This example can be found at http://www.htmlref.com/examples/chapter13/helloworld.php.

Obviously, you might like to do something a little more complicated than the previous example to get the flavor of PHP. Consider the form echo example presented earlier in the chapter; you could use that form but have it post to a PHP program (sayhello.php) instead, as shown here:

  <form method="post"   action="http://www.htmlref.com/examples/chapter13/sayhello.php">   <b>  What's your name?  </b>   <input type="text" name="username" size="25" />   <input type="submit" value="Hi I am..." />   </form>  

The PHP template (sayhello.php) to handle the form would simply be as follows :

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <head>   <title>  Hello from PHP  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   </head>   <body>   <h1 align="center">  Hello from PHP!  </h1>   <hr />   <h1>Hello  <?php  print($POST["username"])  ?>  </h1>   </body>   </html>  

This example is even simpler than the CGI program using a library, and all that was necessary was to print out the posted value from the previous form by referencing the $_POST variables array. Setting your PHP installation to allow global variables you might even avoid that and simply have print($username) . It should be obvious that the beauty of PHP is its simplicity. It also seems easier for those comfortable with markup to put code in their markup rather than markup in their code as in the case of Perl or other technologies. As an example of script in markup or markup in script, compare the earlier example to print environment variables in Perl with a much shorter and cleaner version in PHP:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <head>   <title>  PHP Hello World  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   </head>   <body>  <?php      echo "<b>Environment Variables from $HTTP_ENV_VARS</b><br /><br />";  reset($HTTP_ENV_VARS);  while (list ($key, $val) = each ($HTTP_ENV_VARS))    {    print $key . " = " . $val . "<br />";   } ?>  </body>   </html>  

While offering only a taste of PHP, this discussion should convince readers to visit a site such as http://php.resourceindex.com/ or the primary site www.php.net to find out more information about this popular server-side scripting technology.

Tip  

On Linux distributions with Apache, PHP may be preinstalled ; otherwise , you will most likely have to install, or at the least enable, a PHP module on your Web server. See www.php.net for information on how to download and install PHP for your Web server.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net