PHP is a powerful, server-side scripting language that can be used to extend the functionality of your pages in many ways. It is available for use with Webmaster Accounts and higher, and is activated by naming the files that use it with the extension .php3, .phtml or .php
How does DigiServ.biz run PHP?
DigiServ.biz offers PHP as an Apache module, not as a CGI binary. Thus, clients can take advantage of the benefits of using PHP as an Apache module.
Do I have to have a header at the top of my PHP scripts?
No. Since we are not using PHP as a CGI binary, there is no reason to use the #!/usr/bin.php header in your PHP scripts. Doing so will only cause the header to show up as output as well.
Can I run PHP scripts anywhere on my virtual server?
Yes, PHP scripts do not have to reside in a particular directory and will run anywhere on your virtual server.
What are valid file extensions I can use for PHP scripts?
The valid file extensions for PHP scripts are listed below, these file extensions must be used in order for your PHP scripts to run properly.
The syntax of PHP is very similar to popular programming languages such as C and Perl. If you have any previous programming experience, it is likely that you'll be able to pick up PHP in short order. No one tutorial could ever cover the hundreds of built-in PHP functions, so we highly recommend following and reading over all the related links given at the bottom of this article.
PHP is embedded within a page by enclosing commands within the start tag
Normal HTML tags and text cannot be included within blocks of PHP code. You can output HTML code from within a PHP block to the browser by using the echo command as shown above, or by ending the PHP block, outputting your HTML, and starting a new one. You can include as many separated blocks of PHP code in your file as you need, and any variables you set will be available to the block it was set in, and any block below that.
Variables are assigned in a typical manner. Normal variables are preceded with a $, and assigned to with =. Any variable can be used an array by including a key in square brackets after the variable. For a regular array, a number is used as the key. For an associative array, a quotes enclosed string (or another variable) is used. The function "array" can also be used to create arrays of either type. The code below illustrates these features (lines beginning with "//" are comments):
In the sixth line above, "orange" is assigned to the regular array $fruit with no index given. This tells PHP to assign it the next available number, and thus it becomes assigned to $fruit. One of the most powerful features of PHP is its easy access to form variables. For instance, consider the following short form:
On submitting the form, the value of the variable "formvar" will be available to the form_handler.php3 script simply as $formvar with no parsing required on your part. This is true for both POST and GET form calls.
The form_handler.php3 will also have easy access to all environmental variables in the same manner (the REMOTE_HOST variable can be accessed as $REMOTE_HOST, for instance). This is true of all calls to PHP scripts, not just ones that were originated from a form.
Arithmetic operators in PHP are virtually identical to those in Perl, as illustrated by the example code below:
PHP provides most basic control structures seen in other languages, among them the looping constructs for and while, and the standard if/else construct. Examples are given below:
PHP also allows you full ability to read and write to files in your account. Files are opened via the fopen command, which is given in the form:
where $FILE is the variable you'll use to refer to the open file, filename is the name of the file to be opened, and, mode determines level of access to the file using one of the following values:
- r (read only)
- r+ (read and write)
- w (write only)
- w+ (read and write, truncate file to 0 bytes)
- a (write only, start at end of file -- append)
- a+ (read and write, starting at file end)
If you need to write to the file, the function used is fputs:
After you are done interfacing with the file, you must close your file descriptor with the fclose command:
The sample code below uses the file functions and a while loop to copy the contents of data.txt to newdata.txt. (Not particularly useful in "real life," but good for demonstrative purposes.)
Important Note: As with all CGI file access, the web server must be able to access the files you are trying to read and write to. See the php-cgiwrap documentation for information on making PHP scripts run under your username, and the tutorial on setting file permissions.
A Simple Example:
The HTML and PHP3 code below comprise a simple, self-contained e-mail form. Read over the code first, then read the explanation that follows:
The first time the form is called, the variable $ACTION has no value. This causes the script to output "Hello! Use this form to send me mail!", after which it exits the PHP code block and the rest of the HTML code is outputted as normal, making the mail form.
When the form is filled out and submitted, the page will be called again, but this time all of the form variables and the values the user gave will be available to PHP. By setting $ACTION to "send-mail" using a hidden form variable, the condition of the if statement is made true. The mail function is used to send their message to you via e-mail (see the PHP documentation link below for information on mail), and the message "Thanks for sending me a mail!" is printed. The rest of the HTML then loads again as normal.
For more information on PHP, we highly recommend the related links below. Also, information on interfacing a MySQL database via PHP may be found at our MySQL Tutorial.