PHP Introduction Manual Version 0.2.0 February 18, 2001

Updated to Version 0.5.0 November, 3, 2002

Introduction

PHP stands for Hypertext Preprocessor and is used as a scripting language that is embedded in HTML and runs on the web server. The PHP code is run on the server when the page is requested

Anyone who knows HTML and the C or C++ language can easily program PHP. PHP uses much of the same syntax of C, but is much easier to use and provides access to variables and their values as submitted using HTML forms without the need to parse information. Also PHP is designed to work easily with most SQL servers including the open source SQL server, MySQL.

PHP code is placed in the body of HTML code. It may be placed in HTML code as follows:

<php
echo "This is a test of PHP!";
?>

The above example will output the quoted text on the HTML page where the PHP code is embedded.

Many webservers support PHP out of the box although depending on the version, they may not support the latest version of PHP (Currently 4). If using PHP on the web, it is wise to run the latest version since it has some security enhancements. If you're just using PHP to learn on a local network using the latest PHP version is not as imperative. For example the Apache webserver that comes with Redhat Linux version 6.1 supports PHP3. Depending on how the webserver is setup, it may be required to name the HTML files that contain PHP code with an extension like ".php4".

Documentation

This manual is a brief synopsis of information as an aid to learning PHP. It provides basic PHP information, then provide examples of some useful PHP functionality including sessions, cookie use, sending mail and using it with SQL. It is not intended completely cover all aspects of PHP. Also, it is assumed that the reader is familiar with HTML and languages similar in type to C or Perl. Much PHP syntax is similar to the C programming language. To supplement this document, the reader should refer to the documents page in the weblinks PHP section for available online and downloadable documentation about PHP.

This document and the documentation at the PHP.org Document Section should be all a reader needs to learn PHP. Specifically the large downloadable PHP Manual which is in both PDF and HTML format is an excellent reference manual outlining the many PHP functions by category.

PHP Syntax

PHP statements end with a semicolon.

PHP supports comments the same as C and C++ using the methods shown below:

<?php
// This is a comment
echo "This is a test of PHP!";
?>

<?php
/* Line 1 of this comment
Line 2 of this comment */
echo "This is a test of PHP!";
?>

PHP Variables

Supported variables include integers, floating point numbers, strings, arrays, and objects.

Predefined Variables

There are additional predefined PHP variables defined in a file called "php.ini". Variables defined in HTML forms are available to the PHP file that the form is submitted to using the HTML form "action" attribute. Also if the PHP document has been called using a HTML form, a variable named $submit is available and may be tested to see if it exists. If it exists, an if statement may be used to display one set of HTML rather then the HTML that would be displayed if it does not exist. The phpinfo() function may be used to get a list of predefined variables.

Typecasting

Typecasting is performed with the type written in parenthesis as follows:

$i = (int) $myrealvalue;

Supported typecasts include (int), (array), (object), (string), (real), (double), and (float).

PHP Array Variables

PHP Array variables may be defined in the following manner:

$tree = array("trunk", "branches", "leaves");

In this case the value:

tree[0]

evaluates to the string "trunk". The statement:

$tree[3]="nuts";

sets a fourth array value to "nuts". In this way, the array, tree, may be dynamically expanded.

An array can be created and expanded in the following manner.

$car[ ]="body";
$car[ ]="engine";
$car[ ]="transmission";
$car[ ]="tires";

String Concatenation

Strings can be added to each other or additional values may be added to a string using the append, "." operator as follows:

echo "The value of my variable is: " . $myvariable;

Operators

The math and logical operators are the same as the C programming language. Variables may be pre-incremented or post incremented or decremented with commands like "++$a;" which does a pre increment.

PHP Manual Contents Page