Introduction to PHP

Introduction

If you have ever worked on a web site with more than one page of content, you have probably noticed the drawbacks to coding everything in straight HTML. Things like CSS and JavaScript can prove very helpful in getting what you need accomplished and making your pages easier to update, but if your web site gets to be quite large, and you should always anticipate and prepare for future growth in a site, managing it can become extremely unwieldy. This is where PHP comes in.

History

"PHP" is a recursive acronym that stands for PHP: Hypertext Preprocessor. It began as a product originally called "PHP/FI," created by Rasmus Lerdorf in 1995. PHP/FI was an acronym that stood for Personal Home Page/Forms Interpreter. It was first written in Perl and then, later, in C.

PHP 3.0 was released in June 1998 and was very similar to the PHP of today. It was created as a rewrite by Andi Gutmans and Zeev Suraski because PHP/FI 2.0 was underpowered for the task they needed it to do. PHP 3.0 was announced as the successor to PHP/FI 2.0.

Andi Gutmans and Zeev Suraski began a rewrite of the core of PHP in late 1998. A new engine, the Zend engine, the name of which was made of their two first names, was created and released in 1999. In May 2000, PHP 4.0, the current release of PHP, was released; it is based on the Zend engine.

The future of PHP, PHP 5, truly depends on the engine. The second version of the engine will be incorporated into PHP 5.

Some Benefits

First Steps

Your server has to support PHP in order for you to be able to use it. An easy way to test if you have PHP support is to use the phpinfo() function.

  1. Create a new, blank file.
  2. Inside that file, add this line, verbatim: <?php phpinfo(); ?>
  3. Save the file. Name it whatever you like as long as the extension is php. Something like phpinfo.php makes sense.
  4. Upload the file to the server on which you wish to use PHP.
  5. Try accessing your file on your site via a web browser. If the file won't load like a normal page, simply shows up with the only text as <?php phpinfo(); ?>, or shows up as a blank page, you do not have PHP support. You will have to write to your server and request it be installed or you will have to install it yourself.
  6. However, if a table like this one shows up, you have PHP support! The page that appears will give you information about the version of PHP that is installed.

Simple Implementation

Once you have determined that your server supports PHP, the world is your oyster. PHP is excellent for use in maintaining sections of your site that you wish to be repeated, as I mentioned before. Think of it this way: if you insert CSS into your page in the following manner, you have to insert the same bit on every page:

<style type="text/css">
 <!--
  body {
   background-color: #fff;
   font-family: helvetica, arial, sans-serif;
  }
 -->
</style>

Things will become frustrating quickly if, every time you want to change something in your CSS, you have to go to each individual page to do it. However, if you link to a CSS file in the following manner, you need only update that one file to change the CSS on every page that that file is linked on:

<link href="style.css" rel="stylesheet" type="text/css">

Think of PHP in the same way, only on a much larger scale. Now, I will explain how to easily incorporate PHP into your site.

  1. Create a new, blank file.
  2. Name it whatever you like, as long as it has a .php extension--this will be your page of content. If you're going to have a list of jokes, maybe name it jokes.php.
  3. Set up that page as follows:
<?php
include "vars1.php";
echo $head;
?>

Your page content goes here.

<?php
echo $foot;
?>

Explanation

Continued Implementation

  1. Create another new, blank file. Call it vars1.php. "Vars" stands for variables. I simply prefer using this file name for a file that holds all the variables I incorporate into my pages.
  2. Inside this file, put the following code:
<?php
$head = "
Here, you will put whatever you want above  your content.
";

$foot = "
Here, you will put whatever you want below  your content.
";
?>

In vars1.php, we are defining the variables that we called earlier. Inside your variables, you will have to escape quotes. That means if you have a set of double quotes, like ", you must put a \ in front of them. Look at the following example:

<?php
$head = "
 <html>
  <head>
   <title>My Page</title>
  </head>
  <body>
   <a href=\"http://www.google.com\">Google</a>
";

$foot = "
   <img src=\"smile.gif\" alt=\"Smile\">
  </body>
 </html>
";
?>

Sources / For More Information