条件
$age=18;
//if
if($age>=18){
echo 'adult';//adult
}else{
echo 'child';
}
echo '<br>';
//switch
switch ($age){
case 0:
echo '0';
break;
case 18:
echo 'adult1';//adult1
break;
default:
echo 'child1';
break;
}
echo '<br>';
混编 html 时常见写法
if(true):
echo 'hello world!';
endif;
// 其他条件判断也类似(endif endforeach等)
循环
$arr1=[1,3,5,7,9,11,13];
//for
for ($i=0;$i<count($arr1);$i++){
echo $arr1[$i];//1 3 5 7 9 11 13
echo '<br>';
}
echo '<br>';
//while
$index=0;
while ($index<count($arr1)){
echo $arr1[$index];//1 3 5 7 9 11 13
$index++;
echo '<br>';
}
//do...while
$i = 0;
do {
echo $arr1[$i];//1 3 5 7 9 11 13
$i++;
echo '<br>';
} while ($i < count($arr1))
//foreach
$x = array("one", "two", "three");
foreach ($x as $value) {
echo $value . " ";//one two three
}