Web Hosting Support Center
Using PHP
Home : Hosting Documentation : Using PHP

What is PHP?
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 do you run PHP?
We offer 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.

Using PHP

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 <?php and the end tag ?>

echo "This text written by PHP!\n";

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):

// Normal variable assignment
$person = "Bob";

// Assigning to a regular array
$fruit[0] = "apple";
$fruit[1] = "grape";
$fruit[] = "orange"; // see explanation below

// Assigning to an associative array
$mother['kitten'] = "cat";
$mother['puppy'] = "dog";

// Using the array function for a regular array
$fruit = array('apple','grape','orange');

// Using the array function for an associative array
$mother = array('kitten' => 'cat', 'puppy' => 'dog');

// The following line prints "Bob's cat ate an orange."
echo "$person's $mother['kitten'] ate an $fruit[2].";

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[2]. One of the most powerful features of PHP is its easy access to form variables. For instance, consider the following short form:

<form method=post action="form_handler.php3">
<input type=text name="formvar">
<input type=submit>

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:

Arithmetic operators in PHP are virtually identical to those in Perl, as illustrated by the example code below:

// Addition
$ADD = $VAR + 3;

// Subtraction
$SUB = 18 - 6;

// Multiplication
$MULT = $VAR1 * $VAR2;

// Division
$DIV = 15 / $VAR;

// Modulus (Division to get remainder)
$MOD = $VAR % 2;

Control Structures:

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:

// for Loop Example
for ($I = 0; $I <= 10; $I++) {
print "Current Iteration: $I\n";

// while Loop Example
$I = 0;
while ($I <= 10) {
print "Current Iteration: $I\n";

// if/then/else Example
if ($I > 20) {
echo "$I is greater than 20.\n";
} elseif ($I > 10) {
// notice that PHP uses "elseif" instead of Perl's "elsif"
echo "$I is greater than 10.\n";
} else {
echo "$I is less than 10.\n";

File Access:

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:

$FILE = fopen("filename","mode");
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)

Once the file is opened, there are two commands to read in data. fgetc retrieves a single character, while fgets retrieves a number of bytes you specify. They are used in the following manner:

$ONECHAR = fgetc($FILE);
$TENBYTES = fgets($FILE,10);

If you need to write to the file, the function used is fputs:

fputs($FILE,"This text is written to the file");

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.)

$FILE = fopen("/usr/home/username/data.txt","r"); // open data.txt for reading
$NEWFILE = fopen("/usr/home/username/newdata.txt","w"); // open newdata.txt for writing

// continously read in from data.txt
while ($BUFFER = fgets($FILE,4096)) {
fputs($NEWFILE,$BUFFER); // write line to newdata.txt

fclose($FILE); // close data.txt
fclose($NEWFILE); // close newdata.txt

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:


<TITLE>E-Mail Form</TITLE>


// Your E-mail Address
$MYEMAIL = "you@example.com";

if ($ACTION == "send-mail") {
echo "<h2>Thanks for sending me a mail!</h2>\n";
} else {
echo "<h2>Hello! Use this form to send me mail!</h2>\n";



Message Subject: <INPUT TYPE=TEXT NAME="SUBJECT"><p>






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.

If you would like to experiment with or extend this example, its source code is available as a text file here. Be sure to change the value of $MYEMAIL to your own e-mail address.

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.

Related Links:

  Contains copyrighted material.
Duplication without permission prohibited