Knowledge Base


All content on this website is © Copyright 2000-2010 - All Rights Reserved
The content on this site may not be reused or republished.
Website template powered by VooWeb.com Website Templates

Authoring/Development - Using 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 Advanced level accounts and higher, and is activated by naming the files that use it with the extension .php. Currently, we have PHP 5.2.3 installed on our new Web servers.

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.

How To Include PHP Code

PHP is embedded within a page by enclosing commands within the start tag <?php and the end tag ?>

<?php
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, resuming normal HTML, and later starting a new PHP block.

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.

PHP Variables

Variables are assigned in a typical manner. Normal variables are preceded with a $, and assigned to with =. Any variable can be used in 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):

<?php
// 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].

Access To Form Variables

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.php">
<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.php script simply as $formvar with no parsing required on your part. This is true for both POST and GET form calls.

The form_handler.php 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.

PHP Arithmetic Operators

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

<?php
// Addition
$ADD = $VAR + 3;

// Subtraction
$SUB = 18 - 6;

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

// Division
$DIV = 15 / $VAR;

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

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

<php
// 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";
$I++;
}

// 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";
}
?>

PHP File Access

PHP also allows you full ability to read and write to files in your account. Files ared via the fcommand, which is given in the form:

$FILE = f"filename","mode");

where $FILE is the variable you'll use to refer to thefile, filename is the name of the file to bed, 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 isd, 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:

fclose($FILE);

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

<?php
$FILE = f"/usr/home/username/data.txt","r"); //data.txt for reading
$NEWFILE = f"/usr/home/username/newdata.txt","w"); //newdata.txt for writing

// continuously 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
?>

Related Links:

Please provide feedback on this article.




 
Related Links
» The Basics
» Frequently Asked Questions
» E-Mail
» E-Commerce
» Promotion
» Advertising on Your Site
» Glossary
» Authoring/Development
» About Your Account
» Our Network and Servers
» Billing

» Contact Tech Support

« Knowledge Base Home


 
 
pair Networks - World Class Web Hosting Collections

All content on this website is © Copyright 2000-2010 - All Rights Reserved
The content on this site may not be reused or republished.
Website template powered by VooWeb.com Website Templates

dhhzp.com v 4_3