Webpage Workshop: Home » Articles » Pages On The Fly

Pages On The Fly

By Sharif Tanvir Karim

If you've been using the internet for a while now, you've seen pages where everything happens on one page, and only one page. Ever take the time to look at the url and notice the .php extension and wonder what it meant?

PHP?

PHP is the topic today, ladies and gentlemen. The three little letters that describe one of the most popular coding languages used in over millions of websites all over the net. PHP, stands recursively for PHP Hypertext Preprocessor. One of the main features in PHP is its integration with many types of databases such as the most popular and primary type, MySQL, and other such as mSQL, MS SQL, Sybase, Oracle, and Postgres. You can also parse XML or create WDDX packets. PHP is also a SSI, or a Server-Side-Include. PHP also acts the same as a CGI script does and yes, PHP is a scripting language.

How's it work, doc?

Now, after learning a little about PHP, you are probably wondering, how does it all work? Normally, a Web server goes through to deliver a page to your browser as follows; It begins after your browser makes a request for a Web page. Based on the URL, the browser then resolves the address of the Web server, identifies the page, and gives any information the Web server requests such as text from a form field.

If the request is for a HTML page, Web server will simply output the HTML of that file to the browser. If you have experience with CGI scripts, you know then that after a Web server gets a request for a CGI, it must first execute the script and then send to the browser.

When a PHP page is requested, it is processed like a CGI script, the script is not simply sent to the browser. It is first passed through the "PHP engine", which, in plain english: converts the PHP code to HTML. And what if the user clicks the stop button before the page finishes downloading? Easy, the Web server detects the situation and usually terminates the PHP script. PHP also has the ability to execute special code, even though the script was to be terminated.

The infamous: "Hello World"

So now, you think you know almost everything about PHP and want to give it a shot. Before, you run, you must learn to walk, and so does your server! There are some software requirements to running PHP. PHP requires the Apache Web Server, but you can still use an ISAPI standard software such as Microsoft's Internet Information Server. You also need to choose the right operating system; either a UNIX-like OS, or a Windows based OS, although Windows based OS' aren't suited for high-traffic.

Now, once you are suited to escape HTML, let's learn a little PHP. Like the <html></html> tags used to start and end HTML code, PHP has its OWN start and end tags. The most commonly used tag is <?, which is used to start a PHP code block, and ?> which is used to end a code block.

Below are all the types of PHP start and end tags:

Now that you know how we start it off, lets make our first PHP script, which you can find in almost any tutorial.

In PHP:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php 
      echo "<p>Hello World</p>"; 
    ?>
  </body>
</html>

Now, your first function is introduced to you, the echo() function (not strictly a function, but is known as a language construct). The echo() function outputs a set of strings. The quotes tell PHP that you would like the browser to print text AS IT IS inside the quotes. The ; marks the end of the function.

Now when you view this in a browser, view the source code and it will look like this:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <p>Hello World</p>
  </body>
</html>

Note that the PHP code is left out. Remember I told you the "PHP engine" converts PHP code to regular HTML? This is an example of PHP in action. There are lots of things you can do with PHP. So go explore.

The official PHP website is at http://www.php.net. I recommend you go there first and read the manual, which will help you learn PHP very quickly. Thank you for reading.


The author, Sharif, currently runs http://www.onlyonxbox.net (Only On XBOX), which is a site for Microsoft's XBOX Gaming Console. He loves making PHP scripts and playing video games. He also likes writing lyrics and making web sites that stand out and make you want to stay...