Getting Started with PHP
Posted on 13 December 2003 07:41 PM
What is PHP?
However, PHP is more than a simple scripting language - it has a full set of functions, making it quite powerful. Its most common use is in integrating a web site with a database.
It is assumed that you have a working knowledge of HTML. While not required, it will help you to fully understand how the PHP and HTML work together to bring the user dynamic HTML content.PHP Code
PHP code is enclosed in PHP tags. A PHP tag is opened with either <? or <?php - either one works the same way. PHP tags are closed using ?>.
You can also enclose PHP code like this:
<script language=php> <!-- echo "This is PHP"; --> </script>The above is mainly provided for compatibility with HTML editors, and is not necessarily recommended as it tends to appear more cluttered. Throughout the rest of these tutorials, we will simply use the <? and ?> method.
One of the most basic features of PHP is the Include. The include() function allows you to literally include a file within your PHP page. If the included file is also a PHP script, it can contain code that will be executed.
As an example, a web site can have a common header and footer file, which contain HTML and various functions, that can be used throughout the web site.
Here is a basic example of using includes to make adding pages to a web site very easy:
<? # This is my header file print " <html> <head> <title>My Web Site</title> </head> <body bgcolor=#FFFFFF text=#000000> <center> <table width=580 border=0 cellpadding=0 cellspacing=0> <tr> <td>" ?>This header file starts our 580 pixel wide table, so that our content fits and looks the same at all common screen resolutions.
<? # This is my footer file print " </td> </tr> </table> </center> </body> </html>" ?>Then, each page of our web site can be as simple as this:
<? include "header.php" ?> <center><h2>Welcome!</h2></center> <b>This is my web site!</b> <? include "footer.php" ?>This helps avoid having to add the HTML head to each page and remembering to close the table tags properly. A common use is to have your site navigation links on the top or on the left in a table, which would all go in the header file. Then when you add a page, you only have to edit the header to update the links on all pages!
Note: One thing you must keep in mind is the paths used in an include statement. If header.php is in the same directory as index.php, no path is required. However, if they are in different directories, you will need to specify a path to header.php. This can be a bit tricky...
It will require a full server path so you would simply put:
<? include "/big/dom/xexample/www/includes/header.php" ?>
Whereby you replace xexample with your own xdomain. For example, xmyexampledomain, if your domain is myexampledomain.com.
This should get you started with the include function. Also covered was the print function, which simply outputs what is passed to it, as it does in most languages.
For a complete function reference, see PHP's Function Reference.
Recommended Reading from the FutureQuest Community Forums: The following list of books has been compiled from recommendations made over time by other Site Owners in the Community Forums:
We have provided links to the more recent versions of the books above when last updating this article. If there is an even more recent version, or other PHP and/or MySQL book, you are interested in please feel welcome to ask for specific feedback on it within the forums: