- 作者:霏梦
<?php
// 1、选择判断
$salary=10000;
$year=5;
$adjust_salary=$salary;
// 单分支
if($year<=5){
$adjust_salary=$salary * 0.5;
}
echo '你可以拿到'.$adjust_salary.'工资<br>';
// 双分支
$salary=10000;
$year=6;
$adjust_salary=$salary;
if($year<=5){
$adjust_salary=$salary * 0.5;
}else{
$adjust_salary=$salary * 0.7;
}
echo '你可以拿到'.$adjust_salary.'工资<br>';
// 多分支
$salary=10000;
$year=11;
$adjust_salary=$salary;
if($year<=5){
$adjust_salary=$salary * 0.5;
}elseif($year>5 && $year <=10){
$adjust_salary=$salary * 0.7;
}elseif($year>10 && $year <=20){
$adjust_salary=$salary * 0.9;
}else{
$adjust_salary=$salary;
}
echo '你可以拿到'.$adjust_salary.'工资<br>';
// 简化多分支
$salary=10000;
$year=8;
$adjust_salary=$salary;
switch($year){
case $year <=5:
$adjust_salary=$salary * 0.5;
break;
case $year>5 && $year <=10:
$adjust_salary=$salary * 0.7;
break;
case $year>10 && $year <=20:
$adjust_salary=$salary * 0.9;
break;
default:
$adjust_salary=$salary * 0.9;
}
echo '你可以拿到'.$adjust_salary.'工资<br>';
// switch主要用在单值判断中
$salary=10000;
$year=8;
$adjust_salary=$salary;
switch($year){
case 6:
echo '我上班6年了';
break;
case 7:
echo "我7上班年了";
break;
case 8:
echo "我上班8年了";
break;
default:
echo "我还没有上班";
}
echo '<hr>';
// 2 循环
// while,先判断,为真,就执行
$num=1;
while($num<=10){
echo '$num的值分别是:'.$num.'<br>';
$num++;
}
echo '<hr>';
//do while 先执行,再判断
$num=1;
do{
echo '$num的值分别是:'.$num.'<br>';
$num++;
}while($num<=10);
echo "<hr>";
// for循环初始值,条件,条件增量
for($num=0;$num<=10;$num++){
// if($num>8)
// {
// // exit();
// // break后面的语句不在执行
// break;
// }
if($num==5){
continue;
}
echo '$num的值分别是:'.$num.'<br>';
}
$person=[
['id'=>1,'name'=>'meng1','sex'=>'man'],
['id'=>2,'name'=>'meng2','sex'=>'woman'],
['id'=>3,'name'=>'meng3','sex'=>'--'],];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table border="1" cellspacing='1'>
<tr>
<td>id</td>
<td>姓名</td>
<td>性别</td>
</tr>
<?php for($i=0; $i<count($person);$i++):?>
<tr>
<td width='100'><?php echo $person[$i]['id'] ?></td>
<td width='100'><?php echo $person[$i]['name'] ?></td>
<td width='100'><?php echo $person[$i]['sex'] ?></td>
</tr>
<?php endfor ?>
</table>
</body>
</html>