Contents /
Previous /
Next
Control Statements
if, elseif, else
if (expr) { statements }
elseif (expr) { statements }
else { statements }
switch
switch (expr)
{
case condition1 : {
statements
break; }
case condition2 : {
statements
break; }
default: {
statements }
}
The switch statement executes line by line (actually, statement by
statement). When a case statement is found with a value that matches
the value of the switch expression, PHP begins to execute the
statements. PHP continues to execute the statements until the end of
the switch block, or the first time it sees a break statement. If you
do not write a break statement at the end of a case statement list,
PHP will go on executing the statements of the following case.
Example (two different ways to write the same thing):
if ($i == 0) {
print "i equals 0";
}
if ($i == 1) {
print "i equals 1";
}
if ($i == 2) {
print "i equals 2";
}
switch ($i) {
case 0:
print "i equals 0";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break;
}
Loops
while
while (expr) { statements }
or
while (expr): statements endwhile;
In a do..while loop the first iteration is guaranteed to run
(in contrary to regular while loops):
do
{
statements
} while (condition)
for
PHP for-loops behave like their C counterparts:
for (expr1; expr2; expr3) { statements }
The first expression (expr1) is evaluated (executed) once
unconditionally at the beginning of the loop.
In the beginning of each iteration, expr2 is evaluated. If it
evaluates to TRUE, the loop continues.
At the end of each iteration, expr3 is evaluated (executed).
Each of the expressions can be empty. expr2 being empty means the loop
should be run indefinitely (PHP implicitly considers it as TRUE).
You can end the loop using a conditional break statement.
foreach
The foreach construct is like in Perl.
It gives an easy way to iterate over arrays:
foreach(array_expression as $value) { statements }
or
foreach(array_expression as $key => $value) { statement }
The first form loops over the array given by array_expression. On each
loop, the value of the current element is assigned to $value and the
internal array pointer is advanced.
The second form does the same thing, except that also the current element's
key will be assigned to the variable $key.
When foreach first starts executing, the internal array pointer
is automatically reset to the first element.
foreach operates on a copy of the specified array.
Changes are not reflected in the original array.
The following are functionally identical:
reset ($arr);
while (list(, $value) = each ($arr)) {
echo "Value: $value
\n";
}
foreach ($arr as $value) {
echo "Value: $value
\n";
}
The following are also functionally identical:
reset ($arr);
while (list($key, $value) = each ($arr)) {
echo "Key: $key; Value: $value
\n";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value
\n";
}
Example "list the contents of the array":
$tree = array("trunk", "branches", "leaves");
foreach ($tree as $part)
{
echo "Tree part: $part ";
}
Example " key and value"
$a = array (
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach($a as $k => $v) {
print "\$a[$k] => $v.\n";
}
Example "multi-dimensional arrays":
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach($a as $v1) {
foreach ($v1 as $v2) {
print "$v2\n";
}
}
Example "dynamic arrays":
foreach(array(1, 2, 3, 4, 5) as $v) {
print "$v\n";
}
break
break is used to end the execution of a for, switch, or while statement:
break accepts an optional numeric argument which tells it how many
nested enclosing structures are to be broken out of.
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5
\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting
\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
continue
The continue statement is used to skip the rest of the current loop
and continue execution at the beginning of the next iteration.
while (list ($key, $value) = each ($arr)) {
if ($key % 2) { // skip odd members
continue;
}
do_something_odd ($value);
}
continue also accepts an optional numeric argument which tells it how many
levels of enclosing loops it should skip.
declare and ticks
Declare-Ticks can be used for debugging, implementing simple multitasking,
backgrounded I/O and many other tasks.
The declare construct is used to set execution directives for a block
of code:
declare (directive) { statements }
Currently only one directive is recognized: ticks=N
A tick is an event that occurs for every N low-level statements
executed by the parser within the declare block.
Example:
";
}
// Set up tick handler.
register_tick_function("profile");
// Run a block of code, throw a tick every 7th statement
declare (ticks=7) {
for ($x = 1; $x < 50; ++$x) {
echo "x:".$x."
";
}
}
// De-register tick handler.
unregister_tick_function("profile");
?>