Control Structures
- if
- else
- elseif
- Alternative
syntax for control structures
- switch
- declare
- return
-
-
-
If allows for conditional execution of code fragments.
<?php
if (expression)
statement
?>
Often you'd want to execute a statement if a certain condition is
met, and a different statement if the condition is not met. This
is what else is for. else extends an if statement to execute a statement
in case the expression in the if statement evaluates to FALSE. For
example, the following code would display a is bigger than b if $a
is bigger than $b, and a is NOT bigger than b otherwise:
<?php
if ($a > $b) {
echo "a is bigger than b";
} else {
echo "a is NOT bigger than b";
}
?>
elseif, as its name suggests, is a combination of if and else. Like
else, it extends an if statement to execute a different statement
in case the original if expression evaluates to FALSE. However, unlike
else, it will execute that alternative expression only if the elseif
conditional expression evaluates to TRUE. For example, the following
code would display a is bigger than b, a equal to b or a is smaller
than b:
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
There may be several elseifs within the same if statement. The first
elseif expression (if any) that evaluates to TRUE would be executed.
Alternative syntax for control structures
PHP offers an alternative syntax for some of its control structures;
namely, if, while, for, foreach, and switch. In each case, the
basic form of the alternate syntax is to change the opening brace
to a colon (:) and the closing brace to endif;, endwhile;, endfor;,
endforeach;, or endswitch;, respectively. These alternative syntax
are often used to group mutiple statments together.
<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
The alternative syntax applies to else and elseif as well. The following
is an if structure with elseif and else in the alternative format:
<?php
if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else:
echo "a is neither 5 nor 6";
endif;
?>
switch
The switch statement is similar to a series of IF statements on the same expression.
In many occasions, you may want to compare the same variable (or
expression) with many different values, and execute a different piece
of code depending on which value it equals to. This is exactly what
the switch statement is for.
Note that unlike some other languages, the continue statement
applies to switch and acts similar to break. If you have a switch
inside a loop and wish to continue to the next iteration of the outer
loop, use continue.
switch structure allows usage of strings
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
declare
The declare construct is used to set execution directives for a block
of code. The syntax of declare is similar to the syntax of other flow
control constructs:
declare (directive)
statement
The directive section allows the behavior of the declare block
to be set. Currently only one directive is recognized: the ticks
directive. (See below for more information on the ticks directive)
The statement part of the declare block will be executed --
how it is executed and what side effects occur during execution
may depend on the directive set in the directive block.
The declare construct can also be used in the global scope,
affecting all code following it.
<?php
// these are the same:
// you can use this:
declare(ticks=1) {
// entire script here
}
// or you can use this:
declare(ticks=1);
// entire script here
?>
Ticks
A tick is an event that occurs for every N low-level statements executed by
the parser within the declare block. The value for N is specified using ticks=N
within the declare blocks's directive section.
return
If called from within a function, the return()
statement immediately ends execution of the current function,
and returns its argument as the value of the function call.
return()
will also end the execution of an eval() statement
or script file.
If called from the global scope, then execution of the current
script file is ended. If the current script file was include()ed
or require()ed, then control is passed back
to the calling file.
Furthermore, if the current script file
was include()ed, then the value given to return()
will be returned as the value of the include()
call. If return() is called from within the
main script file, then script execution ends.
|