Today is Wednesday, September 8, 2010
 
Lesson 2
Lesson 2

Introduction to PHP


This section assumes that you know HTML by heart. Even with the overwhelming amount of WYSIWYG editors out there, knowing how to program HTML using a text editor (like Notepad) can increase your understanding of PHP or any other server-side scripting language.
 

PHP is a server-side scripting language used for developing dynamic web pages. PHP actually stands for "PHP: Hypertext Preprocessor". As you can see, PHP is a recursive acronym. It was originally developed by Rasmus Lerdorf as a personal tool to keep track of people who are looking at his online resume. The first version to be publicly released during 1995 was known as Personal Home Page Tools. It consisted of a simplistic parser engine which recognized only a few special macros and special utilities that were used in common home pages back then. Since then, PHP has evolved into a complex yet easy to learn server-side scripting language. It is not merely a hypertext preprocessor anymore due to its ability to generate images and flash application on the fly. PHP was intended to be implemented in a *Nix Machine (Linux, Unix, FreeBSD, etc.) and served via an Apache web server but Windows Binary versions are also available due to the number of Windows users that requested for the release.

Now the question lies ahead. How does one use PHP? PHP is a web-based language. It is embedded in HTML code just like ASP. However, unlike JavaScript, which is also embedded in HTML code, PHP is a server-side programing language while JavaScript is a client-side scripting language. Basically, the difference between these two languages is the fact that the bulk of processing the scripts lies mainly on the server if it is a server-side script. Client-side scripting puts the bulk on the client machines. We will not go into detail about these two types. What we want to get at is the core PHP skills.

PHP is embedded in HTML by using the opening tag "<?" or "<?php" and closing tag "?>" without the double quotes. You can insert this anywhere in an HTML document. For example, consider the undying Hello World PHP script below:

       <html> 
          <head><title>My Hello World Script</title></head>
          <body> 
             <? echo "Hello World!"; ?> 
          </body>
       </html>

This would display "Hello World" with the default font settings on your browser. Take note that ALL statements in PHP must end in a semicolon ( ; ). The echo statement displays one or more strings to the browser. Now let's move on to something which would be of more use to us.

       <html> 
          <head><title>My Date Script</title></head>
          <body> 
             Hello World! Today is 
             <? 
                 echo date("l, F j, Y");
             ?> 
          </body>
       </html>

The script above looks familiar isn't it? It's the same one used throughout this site to display the current day and date. The date function returns formatted date according to the given format. The echo statement shows the formatted date to the browser. To know more about the date function and date formats, please visit the official PHP website.

No programming language is complete without its control structures. PHP uses the C style of implementing control strictures. See the example below:

       <html> 
          <head><title>My Date Script</title></head>
          <body> 
             Hello World! Today is 
             <? 
                 // Display date
                 echo date("l, F j, Y");
             ?> 
             <br>
             
             <?
                 for ($x=1; $x<=7; $x++)
                 {
                   if ($x<4)
                   {
                     print "<font size=\"$x\">These words of size $x are small.</font>";
                   } //end of if
                   elseif ($x == 4)
                   {
                     print "<font size=\"$x\">These words of size $x are medium-sized.</font>";
                   } // end of elseif
                   else
                   {
                     print "<font size=\"$x\">These words of size $x are huge!.</font>";
                   } // end of else
                 } // end of loop
                    
             ?> 
          </body>
       </html>

Don't let the script above scare you. If you would look at it line per line, you would notice that is is actually quite easy to understand. // are used for one-liner comments. You should use /* and */ as your starting mark and ending mark respectively for your multi-line comments.

A typical for statement like the one above needs an initial value for a counter variable, a condition that would determine when the loop would stop, and a counter modifier which either increments or decrements till it meets or exceeds the condition. All variables start with the $ sign. This lessens the chance of using a reserved word for a variable. The print statement is the same as the echo statement although it is more commonly used.

I guess that's it for a PHP introduction. If you want a more detailed documentation regarding PHP, visit the official PHP website.

Close

 

Copyright © 2001 by Vincent Ryan Ong