There are multiple ways to start and stop the PHP processor, but the following is recommended:
Here is an example:
There are three other ways to start and stop processing, but those are not supported on all server configurations. Use the above form whenever possible.
However PHP is a server side interpretive language, so the browser will never see the original PHP source. Thus one cannot save a file PHP file from a browser and see the input to the parser.
PHP can be started and stopped at will, even in the middle of a statement. Consider the following example:
The expression is true.
The expression is false.
Note it is more efficient to stop parsing and revert to HTML then to use
print() for generating static text.
PHP seems to have a fairly loose syntax. For example, function calls may optionally have arguments enclosed in parenthesis:
This call is also valid, too!"); ?>
The reason? PHP actually includes two syntax forms: C style, which requires parenthesis, and Visual Basic style, which does not. This makes it easier for people who know either of those languages to learn PHP.
Instructions are terminated by semicolons. A semicolon is assumed before the closing block, for example:
Keywords are case insensitive. For example,
FALSE all mean the same thing: the PHP keyword
PHP has multiple types of comments, namely:
The closing tag
?> is never considered part of a comment, and always turns off parsing. Incorrect comments may be parsed properly by PHP, but not HTML.
PHP honors the following flat or fundamental types:
PHP honors the following dynamic or reference types:
Variables can store a value from a type or
null, which can simply mean the variable has not been yet assigned.
Variables start with a dollar sign ($) and a letter, then letters or numbers. Unlike keywords, variables are case sensitive.
Within double quoted strings, variables are expanded. Thus the following:
Yields the following HTML output:
Anuni Jason es.
Another form of expansion is escape sequences, for example
\n for newline and
\t for tab. Double quotes allow this expansion. Single quotes do not, except for escaping
\' as a string delimiter. Here is an example:
This results in the following output:
PHP has several system variables. Some start with a dollar sign and an underscore (
$_), which is one reason why user variables must start with a dollar sign and a letter.
An important system variable is
$_POST, which is an array of form data passed via the HTTP POST method. Consider a form with an INPUT item called Anun. PHP can reference this via the system variable
$_POST['Anun']. Here is an example:
You typed in the Anun field on the previous form.
Note in the above the double quoted string allows variable and newline expansion, while the single quoted subscript index (
Anun) does not.
Similar logic works for HTTP GET requests as well using the system
global keyword. For example, the following code:
Yields the following HTML output:
Anun = Varduhi $anun
PHP also offers static variables, which are function variables that preserve their value on every call. Consider the following example:
In the above, the value of
$counter is preserved between function calls. Note that the initial value is zero.
One can define constants within PHP. A constant is like a variable, except constants:
Here is an example PHP block using constants:
Recall that division of two integers with a remainder yields a floating point number, which is different from C.
PHP uses the period for concatenating strings. Concatenation with and without assignment are supported. Here is an example:
Note that the variables
$chors will contain the string
Shadlav! when this code finishes.
PHP honors all of C's relational operators such as
!=. But PHP has two operators made necessary by type juggling. Consider the following code example:
In the above, the
echo function will be called because string variables are automatically converted when compared to integers. To prohibit this automatic conversion, use the triple equals operator, or
===, for an expression to be true only when both type and value are the same. Likewise,
!== is true when at least one of type or value is not the same.
PHP normally will trap errors in functions. To disable error trapping, prepend an expression with the each sign, or
@. Then PHP will continue processing even if an error is encountered, for example a file is not found.
Like shell scripting, PHP allows use of back ticks to call operating system commands. Here is an example:
In addition to standard C bitwise and logical operators, PHP allows the use of keywords
xor as well. Just as with C, PHP operators logically short circuit: if the first comparison in an
&& fails, the second will not be performed.
PHP has two styles for flow control statements: one that follows C style control statements and one that looks more like Visual Basic. However all of the C constructs work as expected. Consequently we will not discuss flow control further in this document.
Since PHP is an interpreted language, it can offer dynamic arrays. To define a variable as an array takes two steps: first, allocate the memory (similar to using
new in C++ or Java). Second, add elements to the array.
To allocate memory, one calls the
array() function. To assign elements, one can use subscript notation. Subscripts can be either integers or strings.
One can access array elements with either subscript notation or the
next() functions. As mentioned previously, form elements can be accessed via the
$_POST system variables using string subscripts.
The following example is rather complex, but demonstrates the use of several array creation and access methods:
|States||Marzer||Gyner||$st\n"; echo "||$marzer[$i]\n"; echo "||$gy\n"; echo "\n"; $st = next($states); $gy = next($gyuner); } ?>|
One powerful feature of PHP is the ability to offer server-side include files. For example, a web page can be built from several components: a masthead, a menu bar, a text body, and a footer. Although HTML does not support an include function, PHP can be used to assemble a web page from several different components.
There are four different include type statements. Here is an example with notes:
Copyright © 2008, Left Brained Geeks. All rights reserved. One or two trivial examples are direct copies from the PHP Manual, Copyright © 1997-2008 the PHP Documentation Group.