Introduction to PHP
"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.
- When you update your site with a change that will affect multiple pages, such as a layout change, you won't have to update each individual page.
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.
- Create a new, blank file.
- Inside that file, add this line, verbatim: <?php phpinfo(); ?>
- Save the file. Name it whatever you like as long as the extension is php. Something like phpinfo.php makes sense.
- Upload the file to the server on which you wish to use PHP.
- 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.
- 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.
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:
font-family: helvetica, arial, sans-serif;
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:
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.
- Create a new, blank file.
- 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.
- Set up that page as follows:
Your page content goes here.
- <?php - This indicates to the server that whatever follows will be PHP.
- include "vars1.php"; - This line adds in the code of another file, which I will show you how to create in a moment, into your current file. We could put all the code that is in the included file into the current file, but by include-ing it, less space is taken up and it allows us to update only vars1.php.
- echo $head; - This echoes a variable (in this case, the variable $head is being echoed), meaning that it places whatever is in the variable into this particular spot. This variable will be defined in our vars1.php file.
- ?> - This indicates to the server that PHP code does not follow this tag. If we need to use PHP again, we can certainly call it up again with <?php, as we do later on in the file.
- In between ?> and <?php is where you will place your page content.
- <?php - We are about to use some PHP so we need this line to inform the server of that.
- echo $foot; - Echoing another variable...
- ?> - Closing the PHP.
- 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.
- Inside this file, put the following code:
$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:
$head = "
$foot = "
<img src=\"smile.gif\" alt=\"Smile\">
Sources / For More Information
In-depth online resource detailing PHP syntax as well as tips and tricks about using PHP