Correcting teacher:Guanhui
Correction status:qualified
Teacher's comments:写的还行!就是代码有点乱!
var_dump(10<20);
var_dump(100=='100');
var_dump(100===100);
var_dump(100==='100');
var_dump(100<=>50);
var_dump(100<=>100);
var_dump(100<=>200);
var_dump((10<20)&&(100<200));
var_dump((10<5)&&(100<200));
var_dump((10<5)||(100<200));
$username='zhangsan';
//如果第一个值为flase,就不会去执行第二个值了。
if (isset($username)&&$username=='admin')
{
echo '登陆成功';
}
else{
echo '登录不成功';
}
$price=1500;
echo $price>1000?'太贵了':'太便宜了';
//如果密码为空,默认为root
$password='123';
echo isset($password)?$password:'root';
echo '<hr>';
echo $email??'10000@qq.com';
$amount=5500;
$payment=$amount;
if($amount>=5000){
$payment=$amount*0.9;
}
echo '实际支付'.$payment.'<br>';
// if($amount>=5000) $payment=$amount*0.9; 流程控制语句只有一行代码,可以这样简写,但不建议,没有可读性。
//模板语法,干掉大括号,也可以像下面这样写。
//第一个大括号用冒号:代替,第二个用特殊关键词代替。
if($amount>=5000):
$payment=$amount*0.9;
endif;
echo '单分支'.$payment.'<hr>';
$amount=6000;
if($amount>=5000){
$payment=$amount*0.9;
}
else{
$payment=$amount;
}
echo $payment.'<br>';
//改造下,改成模板语法
$amount=10000;
if($amount>=5000):
$payment=$amount*0.9;
else:
$payment=$amount;
endif;
echo '双分支'.$payment.'<hr>';
$amount=2000;
if($amount>=5000&&$amount<10000){
$payment=$amount*0.9;
}elseif($amount>=10000&&$amount<20000){
$payment=$amount*0.8;
}elseif($amount>=20000){
$payment=$amount*0.7;
}else{
$payment=$amount;
}
echo $payment.'<br>';
//模板语法改造如下
$amount=22000;
if($amount>=5000&&$amount<10000):
$payment=$amount*0.9;
elseif($amount>=10000&&$amount<20000):
$payment=$amount*0.8;
elseif($amount>=20000):
$payment=$amount*0.7;
else:
$payment=$amount;
endif;
echo '多分支'.$payment.'<hr>';
$amount=22000;
switch (true){ //true表示条件为真,进入循环 也可以用$amount. 变量有数据的话也会被认为成立,就进入下面的代码
case ($amount>=5000&&$amount<10000): //不要括号也可以
$payment=$amount*0.9;
break;
case $amount>=10000&&$amount<20000:
$payment=$amount*0.8;
break;
case $amount>=20000:
$payment=$amount*0.7;
break;
default:
$payment=$amount;
}
echo 'switch分支区间判断'.$payment.'<br>';
//switch 通常用在单值判断
$discount=0.7;
$amount=10000;
switch ($discount){
case 0.9:
$payment=$amount*0.9;
break;
case 0.8:
$payment=$amount*0.8;
break;
case 0.7:
$payment=$amount*0.7;
break;
default:
die('不支持的折扣率');//die() 函数输出一条消息,并退出当前脚本。该函数是 exit() 函数的别名。
}
echo 'switch分支单值判断'.$payment.'<br>';
//模板语法
$discount=0.8;
$amount=10000;
switch ($discount):
case 0.9:
$payment=$amount*0.9;
break;
case 0.8:
$payment=$amount*0.8;
break;
case 0.7:
$payment=$amount*0.7;
break;
default:
die('不支持的折扣率');//die() 函数输出一条消息,并退出当前脚本。该函数是 exit() 函数的别名。
endswitch;
echo 'switch分支单值判断模板语法'.$payment.'<br>';
$cities=['北京','上海','广州','深圳','天津','重庆'];
echo current($cities); //current() 函数返回数组中的当前元素的值。
echo next($cities); //将内部指针指向数组中的下一个元素,并输出。
echo '<font color=red>while循环开始</font>'.'<br>';
while(current($cities)){
echo current($cities).'<br>';
next($cities);
}
echo'<hr>';
reset($cities); //指针复位 指针回到第一条数据。
while($city=current($cities)){ //将当前数组元素赋值给一个变量
echo $city.'<br>';
next($cities);
}
echo'<hr>';
//模板语法
reset($cities);
while($city=current($cities)):
echo $city.'<br>';
next($cities);
endwhile;
echo '<font color=red>do while循环开始</font>'.'<br>';
reset($cities);
do{
echo $city.'<br>';
next($cities);
}while($city=current($cities)); //因为第一次循环 $city没定义,所以第一项数据不会被输出。
echo '<font color=red>for循环开始</font>'.'<br>';
count($cities); //count 返回数组中元素的数目:
for($i=0;$i<count($cities);$i++)
{
//输出拦截
if ($i>2) break; //break提前中止循环
echo $cities[$i].'<br>';
}
echo '<hr>';
//模板语法
reset($cities);
for($i=0;$i<count($cities);$i++):
if ($i===2) continue; //continue中止当条,循环继续执行
echo $cities[$i].'<br>';
endfor;
<?php
$user=[
['id'=>1,'username'=>'zhangsan','sex'=>'男'],
['id'=>2,'username'=>'lisi','sex'=>'女'],
['id'=>3,'username'=>'wangwu','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>
<?php
echo '<table border=1>';
for($i=0;$i<count($user);$i++)
{
echo '<tr><td>'.$user[$i]['id'].'</td><td>'.$user[$i]['username'].'</td><td>'.$user[$i]['sex'].'</td></tr>';
}
echo '</table>';
?>
<!--模板语法来写-->
<table border='1'>
<?php for($i=0;$i<count($user);$i++): ?>
<tr>
<td><?php echo $user[$i]['id'];?></td>
<td><?php echo $user[$i]['username']; ?></td>
<td><?php echo $user[$i]['sex']; ?></td>
</tr>
<?PHP endfor; ?>
</table>
</body>
</html>