What is PHP? How do you run PHP? Do I have to have a header at the top of my PHP
scripts? Can I run PHP scripts anywhere on my virtual server?
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. .php3 .php .phtml 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 ?>
<?php 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: 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):
<?php 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"> 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:
<?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 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:
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); 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 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:
<HTML> 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:
|