PHP is just one of many server-side scripting languages, but one that is widely supported, widely used and an excellent choice for prepocessing documents on the server before they are sent to the browser. PHP is particularly adept at interfacing with a variety of databases.

See these sample files for illustrations of many of the concepts summarized here, and more.

Some History

PHP was developed by Rasmus Lerdorf of the Apache Group. The acronym originally stood for "Personal Home Page", but now it's "PHP: Hypertext Preprocessor".

Here's the PHP timeline:

1995   1997   1998   2000    2004   our version (2011)  ??
1.0    2.0    3.0    4.0     5.0          5.3.2         6.0

Some High-Level Concepts

Debugging

PHP has a function that can be called to set the error-reporting "level" for a script:

error_reporting(level_number);

The default value of level_number is 7. A value of 15 will cause unbound variables to be reported, which might be a useful thing to have in effect.

Some lower-level language details


Here is a table of the four scalar types in PHP (note that string is regarded as a scalar type):

integer like long in C++ or Java
double like double in C++ or Java
boolean true, false
string a sequence of characters enclosed
in single or double quotes

Notes

For the boolean type
For the string type

Some languages, including PHP, contain a rather unusual kind of string construct called a heredoc. Here is an illustration of the heredoc syntax:

$someString = <<<MARKER
    Here is some text delimited by a name I chose (MARKER, in this case) in
    which I can use "double quotes" or 'single quotes', but variables like $x
    will be interpolated. I can even type MARKER here but the heredoc ends
    when I type it at the beginning of a line with a semi-colon immediately
    following ...
MARKER;

Scalar Type Coercions, Conversions and Testing

Here are the basic type coercion/conversion rules for deciding what type a result will have when values of different types are involved in an expression that produces the result:

Coercions

The context usually determines the nature of the coercion.

Conversions (explicit)
Getting or Testing the Type

Here is a table of the two compound types in PHP:

array details
object details

Here is a table of the two special types in PHP:

NULL for variables which have no value
resource can ignore for the time being

Notes on the NULL Type


Here is a list of PHP operators:

Notes:


PHP has selection constructs analogous to those of Java, C and C++, except that the switch-statement permits the use of integer, double or string values ???????:

if (condition)
  statement

if (condition)
  statement1
else
  statement2

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  ...
else
  default_statement
  
switch (expression)
{
  case value1:
    statement1
    break;
  case value2:
    statement2
    break;
  case value3: //Multiple values can cause the same action
  case value4:
    statement_for_either
    break;
  ...
  default:
    default_statement
}

Also elseif ......................

Note that any "statement" in any of the above patterns can be replaced by a compound statement (several statements enclosed in braces).


JavaScript has looping constructs analogous to those of Java, C and C++:

for (initialization; condition; update)
  statement
  
while (condition)
  statement
  
do
  statement
while (condition);

Also foreach ............................

Notes:

  1. Any "statement" in any of the above loop patterns can be replaced by a compound statement (several statements enclosed in braces).
  2. The break; statement in the body of a loop causes the loop to be terminated at that point and execution to continue with the next statement following that loop.
  3. The continue; statement in the body of a loop causes the current loop iteration to terminate and execution to return to the "top" of the loop and carry on with the next iteration.

Output


Programmer-Defined Functions

Here's the syntax for a programmer-defined function:

function name([par_1, par_2, ..., par_n])
{
    statements
    [return value]
}
Notes

Form Handling

PHP has a number of superglobal variables (or, more simply, superglobals):


File Handling


General Notes

  1. Don't use a for-loop with a numerical index to process a PHP array.
  2. PHP version 6 ...