PHP is a server-side scripting language. It runs on UNIX, MS Windows, and Macintosh; it offers fast coding and fast execution; and it is free (including source code). PHP is now a project of the Apache Software Foundation. PHP3 was released in June 1998 and the most recent version 3.0.18. PHP 4 was released in May 2000 and the recent versions are 4.4.8 (January 2008, discontinued) and 5.2.5 (November 2007). Version 5 is a major revision that uses a newer object model that allows object references by handle rather than by value. At UNL, cse is running Version 5.1.2, released in January 2006, as php5. The CSE webserver also runs Version 5.1.2.
See php.net. Reference Core PHP Programming: Using PHP to Build Dynamic Web Sites, Second edition, Leon Atkinson (Prentice-Hall, 2000).
How is PHP used?
A PHP program is included in an HTML file using the
<script>
element or, more simply,
in a tag opening with "<?" or "<?php" and closing
with "?>". For example (adapted from [Atkinson]):
<html>
<head>
<title>PHP Example
1</title>
</head>
<body>
<h1>Today's date is <script
language="php">print(Date("F d, Y"));</script>.</h1>
Today's date is <?php
print(Date("F d, Y")); ?>.
Today's date is <? print(Date("F d, Y")); ?>.
</body>
</html>
See phpex1.php and phpex1.txt.
The PHP program is interpreted by the server and the output of the PHP program is incorporated in the HTML document at the point of the script when the HTML document is passed to the browser (or other client side program).
Language elements
White space and comments enclosed between "/*" and "*/" in PHP programs are ignored.
PHP is case sensitive (except built-in function names). Identifiers are of the form /[_A-Za-z][_A-Za-z0-9]*/.
Variable names are preceded by a dollar sign. For example:
$month = "September";
print("<em>The month is
$month.<em>\n");
Variables need not be declared. They are implicitly string, integer, or floating point and are recast dynamically. The type can be recast explicitly using the strval, intval, or doubleval functions and the implicit type can be reset using the settype function. Functions have their own variable space and variables have a defined scope. There is support for global and static variables.
PHP has ordinary, C-like arithmetic, logical, relational, bitwise, assignment, and string operators. For example:
$date .= ", 2000";
PHP has ordinary, C-like control structures such as if-then-else, for, while, do-while, and switch. For example:
for ($cnt = 0; $cnt < $cntmx;
$cnt++)
{
print("$cnt
Misssissippi.\n");
}
PHP has ordinary, C-like arrays, multi-dimensional arrays, and Perl-like associative arrays.
$John["birthdate"] = $date;
$John["anniversary"][] = $date;
PHP has objects, defined with the class declaration, instantiated with the new statement, and method access with the "->".
class student {
var $name;
function
init($inputName)
{
$this->name = $inputName;
}
}
$curstudent = new student;
$student->init("John Wu");
There are many, many builtin functions for I/O (including network I/O), string processing, system and HTTP operations, mathematics, time and date, graphics and images, and databases.
Server-side input
PHP programs can access values provided by HTML form elements through predefined variables. In older versions of PHP, posted values were available directly through variables of the same name. In newer versions of PHP, these values are available through autoglobal variable lists. For example, see examples using the $_REQUEST list, adapted from [Atkinson] 7-2.php and 7-2.txt.
PHP programs also can receive file uploads via the HTML post method of the form element. Here is a form from [Atkinson]:
<FORM ENCTYPE="multipart/form-data"
ACTION="7-3.php"
METHOD="post">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE"
value="4096">
<INPUT NAME="UploadedFile" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
In the server-side program named
7-3.php (7-3.txt),
the file
field results in several PHP variables, including:
$_FILES['UploadedFile']['tmp_name'] | The local file system path and location of the uploaded file . |
$_FILES['UploadedFile']['name'] | The file name provided by the browser. |
$_FILES['UploadedFile']['size'] | The file size in bytes. |
$_FILES['UploadedFile']['type '] | The MIME type of the file as specified by the browser. |
So, the PHP script could be:
<?
$UploadedFile
= $_FILES['UploadedFile']['tmp_name'];
$UploadedFile_name
= $_FILES['UploadedFile']['name'];
$UploadedFile_size
= $_FILES['UploadedFile']['size'];
$UploadedFile_type
= $_FILES['UploadedFile']['type'];
$UploadedFile_error
= $_FILES['UploadedFile']['error'];
//check for
file upload
if(is_uploaded_file($UploadedFile))
{
print("Local File: $UploadedFile <BR>\n");
print("Name: $UploadedFile_name <BR>\n");
print("Size: $UploadedFile_size <BR>\n");
print("Type: $UploadedFile_type <BR>\n");
print("<HR>\n");
} else {
print("Type: $UploadedFile_error <BR>\n");
}
?>
PHP also has access to environment variables. These include both ordinary Unix-like variables such as PATH and CGI variables such as HTTP_USER_AGENT. The GLOBALS variable is an associative array of all global variables. Other variable arrays, such as $_ENV, $_SERVER, $_REQUEST, and $_POST, provide access to subsets of the global variables. For globals.php (globals.txt),
<?
for(reset($GLOBALS);
$keys = key($GLOBALS); next($GLOBALS)) {
print("GLOBALS $keys<BR>\n");
$lst = $GLOBALS[$keys];
for(reset($lst); $key = key($lst); next($lst)) {
print("- $key = $lst[$key]<BR>\n");
}
}
?>
Server-side files can be accessed via include and require. The require statement causes the actual replacement of the statement with the file contents.
There are many functions for file access including fopen,
fclose,
chmod,
etc. Files can be internet
accessible files beginning with http:// or ftp://.
There is also support for pipes and sockets.
PHP can execute shell commands.