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
- 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.
- JavaScript can be disabled in a visitor's browser or not supported at all. With PHP, you can accomplish many things that JavaScript can do without the drawback of the possibility of your code not working for everyone. The only thing that has to support PHP is your server; on a browser-by-browser basis, it does not matter.
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.
- 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.
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:
<!--
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:
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:
include "vars1.php";
echo $head;
?>
Your page content goes here.
<?php
echo $foot;
?>
Explanation
- <?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.
Continued Implementation
- 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 = "
<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
- PHP.Net
http://www.php.net
In-depth online resource detailing PHP syntax as well as tips and tricks about using PHP