Tutorial - What Is PHP? A beginner’s primer
Author:  webbrothers - W. Klapwyk        Email:  wklapwyk@webbrothers.ca
      Return to Archives

Page: 1 of 1
In order to understand what PHP is and what it can do for your website there are a couple of concepts that must be addressed before delving into the mysteries of web programming.

First, PHP is a server side scripting language that is preceded by the start marker '<?PHP' and ends with the marker '?>'. As a server side script and unlike HTML and Javascript, PHP is run from the server instead of in the client’s web browser and is completely invisible to the client machine. There are some definite benefits to this as well as certain drawbacks. First let’s deal with the benefits.

Chief among the benefits is the fact that what any given PHP script is doing will be hidden from the final output in the client browser. The visitor to your site will only see what you want them to see and nothing more. This allows you to write complex scripts that can do anything from simply replacing text on a page to processing database requests without any serious risks to security.
  For Example:
  <?PHP
    $outVar = "webbrothers.ca";
  ?>
  <a href="http://www.<?PHP echo $outVar; ?>" target=\"_blank\">go to <?PHP echo $outVar; ?>

  Will output the following to the browser:     go to webbrothers.ca
A second benefit of a server side language is that all processing of the scripts are done before any output is presented to the user. This allows for completely dynamic redrafting of your HTML behind the scenes without anyone even knowing what is going on. For example, a simple HTML page can be split so that the header, menu, and footers are all stored in different files, then when writing the various pages of your website you only need to change the content of the body and "include" the other external parts. The result is a single HTML document presented to the user's browser.
  For Example the following code:
  <?PHP
    // include the header and menu portions of the page
    include "inc/header.php";
  ?>
  <body bgcolor="#547ED9" LEFTMARGIN=0 TOPMARGIN=0>
  -- This is where the content of your page goes --
  </body>
  <?PHP
    // include the page footer
    include "inc/footer.php";
  ?>

  Produces:
  <!-- Start Header.php -->
  <html>
    <head>
      <title>webbrothers Example Page</title>
    </head>
  <!-- End Header.php -->
  <!-- Start Page Content -->
    <body bgcolor="#547ED9" LEFTMARGIN=0 TOPMARGIN=0>
      -- This is where the content of your page goes --
      <!-- Start Footer.php -->
      <p align=center>©2006 webbrothers Internet Design</p>
    </body>
  </html>
  <!-- End of Footer.php -->
PHP can also be included inline with regular HTML or Javascript code presenting another powerful advantage to your site. This allows variables passed from a preceding page or retrieved from a configuration file or database to be dynamically inserted into the body of any given page on your site. For instance, let’s say that a visitor to your site has to "log in" to use your site. You may wish to drop a cookie storing specific information about this user including their name. This name could then be placed dynamically at the top of each succeeding page using a simple statement in PHP.
  For Example:
  <?PHP
     session_save_path("/home/users/web/b2355/ipw.webbroth/phpsessions"); session_start();
    // retrieve the cookie data
    if (isset($_SESSION['cUser'])) {
      $logUser = $_SESSION['cUser'];
    } else {
      $logUser = "";
    }
    // include the header and menu portions of the page
    include "inc/header.php";
    include "inc/menu.php";
  ?>
  <body bgcolor="#547ED9" LEFTMARGIN=0 TOPMARGIN=0>
  <font color="red">Welcome back <?PHP echo $logUser; ?>
  -- This is where the rest of the content of your page goes --
  </body>
  <?PHP
    // include the page footer
    include "inc/footer.php";
  ?>
Using PHP also opens up the whole area of database usage on the internet. PHP can plug natively into the MySQL database or connect to any number of other databases using standard connection technologies like ODBC and ADO. This database access allows developers to store all sorts of information in a back-end repository for use anywhere within their site. This power can lead to the development of a whole host of online applications and games. Many of the leading Chat rooms, Photo Galleries, Search Engines and online retail stores rely on a back-end databases to provide fast, reliable information with a minimum of programming.
  For Example:
  <?PHP
    // Set the database variables
    $username = "pee_wee";
    $password = "let_me_in";
    $hostname = "webbroth.ipowermysql.com";
    // Connect to the database
    $dbh = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");
    print "Connected to MySQL";
    // retrieve some information
    $selected = mysql_select_db("first_test",$dbh)
      or die("Could not select first_test");
    mysql_close($dbh);
  ?>
The only major draw back to using PHP or any other server side scripting language also arises from the fact that they are all run on a server and not in the client’s browser. This means that you can not change any information on the page based on a selection made by the user without refreshing the page. In the normal course of basic website development you may never need to use such functionality. However, should you require it then in most instances Javascript can be used to provide this functionality.

If you truly do require the power and flexibility of a server side language combined with client side scripting more and more people are turning to the AJAX language which stands for "Asynchronous Javascript And XML" and can be used to create some truly outstanding results. However, like Java and other complex languages it can also be a very convoluted and difficult to master.


      Return to Archives

 

  webbrothers Home   |   About Us   |   Services   |   Article Archives   |   Contact Us   |   Site Map