for ($i = 4; $i < 8; $i++) {
print "I have eaten $i bagels today.\n"; }
產生如下結果
I have eaten 4 bagels today.
I have eaten 5 bagels today.
I have eaten 6 bagels today.
I have eaten 7 bagels today.
還有以下程式碼也會產生同樣結果
$i = 4; while ($i < 8) {
print "I have eaten $i bagels today.\n";
$i++;
}
你可以利用控制結構 if 以及 elseif:
if ($user_count > 200) {
print "The site is busy right now!";
} elseif ($user_count > 100) {
print "The site is sort of active right now!";
else {
print "The site is lonely - only $user_count user logged on.";
}
運算元(operator)利用的最高指導原則同樣也可以運用在控制結構上面. 你還可以使用 switch, do...while,甚至是 ?: 程式結構。
<?php
function process_form() {
global $name;
global $cheese;
global $times;
if ($cheese == 'brie') { $cheese_message = 'I love brie.'; }
elseif ($cheese == 'cheddar') { $cheese_message = 'Cheddar is awesome!'; }
else { $cheese_message = 'Fresh mozzarella is divine.'; }
$favorite_times = count($times);
if ($favorite_times <= 1) {
$times_message = 'You should eat cheese more often.';
} elseif ($favorite_times > 1 && $favorite_times < 4) {
$times_message = 'Those are good times to eat cheese.';
} else {
$times_message = 'You are eating too much cheese.';
}
$formatted_first = date('F d, Y',$timestamp);
$formatted_next = date('F d, Y',$next_timestamp);
echo "The first $dotw after $formatted_first is $formatted_next.";
}
?>
<?php
$today = mktime(12,0,0,6,25,1999);
echo 'Here it is '.date('g:i:s a, F d, Y',$today);
echo '';
echo 'In GMT it is '.gmdate('g:i:s a, F d, Y',$today);
?>
會產生如下結果
Here it is 12:00:00 pm, June 25, 1999
In GMT it is 4:00:00 pm, June 25, 1999