UNIT 1 - Flow-Control Statements (Programming PHP)
UNIT 1 - Flow-Control Statements (Programming PHP)
home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
2.5.1. if
The if
statement checks the truthfulness of
an expression and, if the expression is true, evaluates a statement.
An if statement looks like:
if (expression)
statement
if (expression)
statement
else
statement
For example:
if ($user_validated)
echo "Welcome!";
else
echo "Access Forbidden!";
if ($user_validated) {
echo 'Welcome!";
$greeted = 1;
} else {
echo "Access Forbidden!";
exit;
}
PHP
provides another syntax for blocks in tests and loops. Instead of
enclosing the block of statements in curly braces, end the
if line with a
colon (:) and
use a specific keyword to end the block (endif, in
this case). For example:
if ($user_validated) :
echo "Welcome!";
$greeted = 1;
else :
echo "Access Forbidden!";
exit;
endif;
<?if($user_validated):?>
<table>
<tr>
<td>First Name:</td><td>Sophia</td>
</tr>
<tr>
<td>Last Name:</td><td>Lee</td>
</tr>
</table>
<?else:?>
Please log in.
<?endif?>
Because if
is a statement, you can chain
them:
if ($good)
print('Dandy!');
else
if ($error)
print('Oh, no!');
else
print("I'm ambivalent...");
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 1/6
7/14/2021 Flow-Control Statements (Programming PHP)
Such chains of if statements are common enough
that PHP provides an easier syntax: the elseif
statement. For example, the previous code
can be rewritten as:
if ($good)
print('Dandy!');
elseif ($error)
print('Oh, no!');
else
print("I'm ambivalent...");
The main
difference here is that the conditional operator is not a statement
at all. This means that it is used on expressions, and the result of
a
complete ternary expression is itself an expression. In the
previous example, the echo statement is inside the
if condition, while when used
with the ternary
operator, it precedes the expression.
2.5.2. switch
if ($name == 'ktatroe')
// do something
elseif ($name == 'rasmus')
// do something
elseif ($name == 'ricm')
// do something
elseif ($name == 'bobk')
// do something
switch($name) {
case 'ktatroe':
// do something
break;
case 'rasmus':
// do something
break;
case 'ricm':
// do something
break;
case 'bobk':
// do something
break;
}
switch($name):
case 'ktatroe':
// do something
break;
case 'rasmus':
// do something
break;
case 'ricm':
// do something
break;
case 'bobk':
// do something
break;
endswitch;
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 2/6
7/14/2021 Flow-Control Statements (Programming PHP)
switch ($name) {
case 'sylvie': // fall-through
case 'bruno':
print('yes');
break;
default:
print('no');
break;
}
You can specify an optional number of levels for the break keyword to
break out of. In this way, a
break statement can break out of several
levels of nested switch statements. An example of
using break in this manner is shown in the next
section.
2.5.3. while
The
simplest form of loop is the while statement:
while (expression)
statement
$total = 0;
$i = 1;
while ($i <= 10) {
$total += $i;
}
while (expr):
statement;
...;
endwhile;
For example:
$total = 0;
$i = 1;
while ($i <= 10):
$total += $i;
endwhile;
$total = 0;
$i = 1;
while ($i <= 10) {
if ($i == 5)
break; // breaks out of the loop
$total += $i;
$i++;
}
$i = 0;
while ($i < 10) {
while ($j < 10) {
if ($j == 5)
break 2; // breaks out of two while loops
$j++;
}
$i++;
}
echo $i;
echo $j;
0
5
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 3/6
7/14/2021 Flow-Control Statements (Programming PHP)
if ($j = 5)
continue 2; // continues through two levels
$j++;
}
$i++;
}
do
statement
while (expression)
$total = 0;
$i = 1;
do {
$total += $i++;
} while ($i <= 10);
do {
// do some stuff
if ($error_condition)
break;
// do some other stuff
} while (false);
2.5.4. for
$counter = 0;
while ($counter < 10) {
echo "Counter is $counter\n";
$counter++;
}
The expression
start is evaluated once, at the beginning
of the for statement. Each time through the loop,
the expression condition is tested.
If it
is true, the body of the loop is executed; if it
is false, the loop ends. The expression
increment is evaluated after the loop body
runs.
$total = 0;
for ($i= 1; $i <= 10; $i++) {
$total += $i;
}
$total = 0;
for ($i = 1; $i <= 10; $i++):
$total += $i;
endfor;
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 4/6
7/14/2021 Flow-Control Statements (Programming PHP)
$total = 0;
for ($i = 0, $j = 0; $i <= 10; $i++, $j *= 2) {
$total += $j;
}
for (;;) {
echo "Can't stop me!<br />";
}
2.5.5. foreach
The foreach
statement allows you to iterate over elements in an
array. The two
forms of foreach statement are discussed in Chapter 5. To
loop over an array, accessing each key,
use:
2.5.6. declare
declare (directive)
statement
register_tick_function("some_function");
declare(ticks = 3) {
for($i = 0; $i < 10; $i++) {
// do something
}
}
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 5/6
7/14/2021 Flow-Control Statements (Programming PHP)
2.4. Expressions and Operators 2.6. Including Code
https://docstore.mik.ua/orelly/webprog/php/ch02_05.htm 6/6