<?php
// 单分支
$name = 'pharaoh';
if ($name === 'pharaoh')
{
echo $name;
}
echo '<br>';
// 双分支
$num = 30;
if ($num >= 30)
{
echo sprintf('你已经%s岁了',$num);
}
else {
printf('你还小不能访问');
}
echo '<br>';
// 三元运算符
$num = 17;
echo $num >= 30 ? sprintf('你已经%s岁了',$num) : '你还小不能访问';
echo '<hr/ >';
// 多分支
$sex = 'male';
if ($sex === 'male') {
echo '你是男性';
}elseif ($sex === 'female') {
echo '你是女性';
}else {
echo '你是男是女?';
}
echo '<hr/>';
// switch
$time = 45;
switch($time) {
case $time <= 60 && $time !== 0:
echo 'You are best!!';
break;
case $time >= 60 && $time <= 80:
echo 'Come on!!';
break;
default:
echo 'looser!';
}
模板语法
<!-- 模板语法 -->
<?php foreach($arr as $values): ?>
<?='<tr>' ?>
<td><?=$values['name']?></td>
<td><?=$values['sex']?></td>
<td><?=$values['power']?></td>
<?='</tr>' ?>
<?php endforeach?>
<?php foreach ($arr as $values): ?>
<?php if($values['power'] === 0):?>
<?= '<tr style="color:red">'?>
<?= '<td>' . $values['name'] . '</td>'?>
<?= '<td>' . $values['sex']. '</td>'?>
<?= '<td>' . $values['power']. '</td>'?>
<?= '</tr>'?>
<?php endif?>
<?php endforeach?>