Chapter 1. PHP Pocket Reference
Installation and Configuration
PHP works with many different web servers in many different ways, butby far the most popular way to run PHP is as an Apache module withApache 1.3.x. Full installation instructions for all the differentways to install PHP can be found in the PHP documentation. Here, Icover the Apache module installation.
If you are compiling from the PHP source tarball, follow theinstructions in the INSTALL file found insidethe PHP distribution file. A tarball is a compressed tar file. tar stands fortape archive, but these days it has little to do with tapes. It issimply a way to lump multiple files and directories into a singlefile for distribution. Normally tarballs have the .tar.gz extension to indicate a tar file compressed with gzip . To untar a tarball, use:
tar zxvf foo.tar.gz
On Windows, many utilities (including WinZip) understand tarballs.
If you are installing from a precompiled binary package such as an rpm file, most of the work should be done foryou. But doublecheck that the Apache configuration described below iscorrect.
When you are using PHP as an Apache module, PHP processing istriggered by a special MIME type. This is defined in the Apacheconfiguration file with a line similar to:
AddType application/x-httpd-php .php
This line tells Apache to treat all files that end with the .php extension as PHP files, which means thatany file with that extension is parsed for PHP tags. The actualextension is completely arbitrary and you are free to change it towhatever you wish to use.
If you are running PHP as a dynamic shared object (DSO) module, youalso need this line in your Apache configuration file:
LoadModule php4_module modules/libphp4.so
Note that in many default httpd.conf files youwill find AddModule
lines. These reallyaren't necessary. They are only needed if you have aClearModuleList
directive somewhere in your httpd.conf file. I would suggest simply deletingthe ClearModuleList
directive and deleting allyour AddModule
lines. The idea behindClearModuleList
/AddModule
is tomake it possible to reorder already loaded modules in case moduleorder is an issue. With most modules, the order that they areloaded -- which governs the order they are called -- is notimportant. And further, most binary distributions of Apache ship withmost modules compiled as dynamically loadable modules, which meansthat if order is an issue for some reason, you can simply change theorder of the LoadModule
calls to fix it.
Don't forget to restart your server after makingchanges to your httpd.conf file. Once the serveris restarted, you can check to see if PHP is working by creating afile in your document root named info.php containing the single line:
Load this up in your browser using http://your.domain.com/info.php . You should seeall sorts of information about PHP. If you don't seeanything, try selecting "ViewSource" in your browser. If you see thephpinfo(
) line, you probably forgot (or mistyped)the AddType
line in your httpd.conf file. If the browser tries todownload the file instead, it means that theAddType
is there, but the PHP module is not beingtriggered -- perhaps because you forgot theLoadModule
line.
Once you have verified that PHP is working, have a look at the PHPinitialization file called php.ini . Thephpinfo( )
page will tell you where PHP isexpecting to find it. PHP functions fine without this file, but withall the default settings. If you want to change the defaults, orperhaps more importantly, you want to be immune from any changes tothe defaults when you upgrade, you should create a php.ini file. The source distribution of PHPcomes with a php.ini-dist file that you canrename and copy into the location specified in the phpinfo()
output. The php.ini file itself iswell-commented and self-explanatory for the most part.
You can also put configuration directives inside the Apache httpd.conf file, and, in certain cases, inindividual .htaccess files. This is very usefulfor setting things per-directory or per-virtual host. If you havethis line in the php.ini file:
include_path = ".:/usr/local/lib/php:.."
you can set this in your httpd.conf file with:
php_value include_path .:/usr/local/lib/php:..
There are four httpd.conf directives used forsetting PHP directives:
php_value
For setting normal strings and values
php_flag
For setting boolean values
php_admin_value
For setting administrative values
php_admin_flag
For setting boolean administrative values
In addition, the normal values and booleans can be set in your .htaccess files, but only if the ApacheAllowOverride
setting (which sets what is allowedin a .htaccess file) includes"Options"
.
More information can be found at http://www.php.net/configuration.
Embedding PHP in HTML
You embed PHP code into a standard HTML page. For example,here's how you can dynamically generate the title ofan HTML document:
...
The portion of the documentis replaced by the contents of the $title
PHPvariable. echo
is a basic language statement thatyou can use to output data.
There are a few different ways to embed your PHP code. As you justsaw, you can put PHP code between and?>
tags:
echo "Hello World"; ?>
This style is the most common way to embed PHP, but it is a problemif your PHP code needs to co-exist with XML, as XML may use thattagging style itself. If this is the case, turn off this style in the php.ini file with theshort_open_tag
directive. Another way to embed PHPcode is within
This style is always available and is recommended when your PHP codeneeds to be portable to many different systems. Embedding PHP within tags is another style that isalways available:
echo "Hello World";
One final style, in which the code is between and %>
tags, isdisabled by default:
You can turn on this style with the asp_tags
directive in your php.ini file. The style ismost useful when you are using Microsoft FrontPage or another HTMLauthoring tool that prefers that tag style for HTML-embedded scripts.