if-else结构
除了if()语句外,PHP也提供if-else结构,该if-else结构用于定义一代码块,该代码块在if()语句中的条件表达式值为假时而得到执行。if-else结构看起来像下面这样:
if (condition) {
do this!
}
else {
do this!
}
该结构可在上一例子中得到极为有效的使用:我们可以将两个单独的if()语句合并为一个单一的if-else语句。
<html>
<head></head>
<body>
![]()
<?php
// retrieve form data
$age = $_POST['age'];
// check entered value and branch
if ($age >= 21) {
echo 'Come on in, we have alcohol and music awaiting you!';
}
else {
echo "You're too young for this club, come back when you're a little older";
}
?>
![]()
</body>
</html>
