基本和C相同。不过可以使用一些变体。
control.html
<html>
<head>
<title>php控制结构</title>
</head>
<body>
<form action="control.php" method="POST">
Your Name : <input type="text" name="name"><Br>
Choose one: <select name="selection">
<option value="a" selected>I'm a man!</option>
<option value="b" selected>I'm a woman!</option>
</select>
<input type="submit">
</form>
</body>
</html>
control.php
<?php

# process name
$name=$_REQUEST["name"];
if (empty($name)) { # 也可以不使用代码块 // 或者使用 : ... endif;
# 这里不能使用is_null
echo "Enter your name, please!<br>";
} elseif (trim($name)=="great") {
echo "Your are great!<br>";
} else {
echo "Hello, $name!<br>";
}

# process selection
switch ($_REQUEST["selection"]) { // 或者使用 : ... endswitch;
case "a":
echo "You are a man!<br>";
break; // break同C的break(也可用于循环), exit结束整个脚本的执行
case "b":
echo "You are a woman!<br>";
break;
default:
echo "What thing are you?<br>";
break;
}
# while
echo("while: ");
$i=1;
while ($i<=5) : // 或者使用大括号
echo "$i ";
$i++;
endwhile;
echo("<br>");
# for
echo("for: ");
for ($i=0;$i<5;$i++) echo("$i "); // 可以使用 : ... endfor;
echo("<br>");
# foreach
# nothing here
# do ... while
$i=1;
echo("do ... while: ");
do {
echo "$i ";
$i++;
} while ($i<=5);
?>
control.html















control.php


















































