Creating PHP scripts

A PHP script can be written using plain text[1] and can be created with any text editor, such as joe, vi, nedit, emacs, or pico.

[1] While printable characters with the most significant bit are allowed, PHP scripts are usually written using characters from the 7-bit ASCII character set.

If you save a PHP script in a file with a .php extension under the directory configured as Apache's document root, Apache executes the script when a request is made for the resource. Following the installation instructions given in Appendix A, the document root is:


Consider what happens when the script shown in Example 2-1 is saved in the file:


Apache-when configured with the PHP module-executes the script when requests to the URL http://localhost/example.2-1.php are made, assuming the web browser is running on the same machine as the web server.

If directory permissions don't permit creation of files in the document root, it's also possible to work in the user home directories. If the installation instructions in Appendix A have been followed, a directory can be created by a user beneath her home directory and the permissions set so that the directory is readable by the web server:

mkdir ~/public_html
chmod a+rx ~/public_html

The example file can then be created with the filename:


The file can then be retrieved with the URL http://localhost/~user/example.2-1.php, where user is the user login name.

PHP Comments

Comments can be included in code using familiar styles from other high-level programming languages. This includes the following styles:

// This is a one-line comment
#  This is another one-line comment style
/* This is how you
   can create a multi-line
   comment */