批改状态:合格
老师批语:三篇作业代码写的还行,就是文字略少。
var_dump(10 > 20);var_dump(100 == '100');//返回truevar_dump(100 === '100');//返回false,因为要求类型必须一致。
var_dump(10 <=> 20);var_dump(10 <=> 5);var_dump(10 <=> 10);
var_dump((10 > 20) && (5 > 8));var_dump((10 < 20) || (5 > 8));var_dump(true || false);
$username = 'peter';if ($username && $username === 'admin')echo '欢迎您';elseecho '您未登录,请先登录';echo '<hr>';
$price = 2000;if ($price > 1500) echo '太贵了';else echo '太便宜了';// 可写成:下面这种方式$price = 1000;echo $price > 1500 ? '太贵了' : '太便宜了';echo '<hr>';// root, 密码:默认就是 root$password = sha1('123456');echo isset($password) ? $password : 'root';echo '<hr>';// isset() + 三元运算 = 变量默认值// $password = '123456';// 进一步简化为??// ?? : null合并运算符unset($password);echo $password ?? 'root';
$amount = 4500;// 实际支付的金额,默认值是不打折的金额$payment = $amount;// if ($amount >= 5000) {// 当流程控制语句中只有一行代码的时候,可以省略大括号// $payment = $amount * 0.9;// }// if ($amount >= 5000) $payment = $amount * 0.9;
if ($amount >= 5000) :// 当流程控制语句中只有一行代码的时候,可以省略大括号$payment = $amount * 0.9;endif;if ($amount >= 5000) : $payment = $amount * 0.9; endif;echo '实际支付: ' , $payment , '<br>';echo '<hr>';
$amount = 4500;if ($amount >= 5000) {$payment = $amount * 0.9;} else {$payment = $amount;}
$amount = 7500;if ($amount >= 5000) :$payment = $amount * 0.9;else :$payment = $amount;endif;echo '实际支付: ' , $payment , '<br>';echo '<hr>';
$amount = 22500;if ($amount > 5000 && $amount < 10000) {$payment = $amount * 0.9;}elseif ($amount >= 10000 && $amount < 20000) {$payment = $amount * 0.7;}elseif ($amount >= 20000) {$payment = $amount * 0.5;}else {// 默认值$payment = $amount;}
if ($amount > 5000 && $amount < 10000):$payment = $amount * 0.9;elseif ($amount >= 10000 && $amount < 20000) :$payment = $amount * 0.7;elseif ($amount >= 20000) :$payment = $amount * 0.5;else :// 默认值$payment = $amount;endif;echo '实际支付: ' , $payment , '<br>';echo '<hr>';
// switch (表达式) {// case 条件1:// 分支语句...// break;// case ...// default:// ....// }// if ($amount > 5000 && $amount < 10000):// $payment = $amount * 0.9;// elseif ($amount >= 10000 && $amount < 20000) :// $payment = $amount * 0.7;// elseif ($amount >= 20000) :// $payment = $amount * 0.5;// else :// $payment = $amount;// endif;$amount = 3500;switch (true) {case $amount > 5000 && $amount < 10000:$payment = $amount * 0.9;break;case $amount >= 10000 && $amount < 20000 :$payment = $amount * 0.7;break;case $amount >= 20000 :$payment = $amount * 0.5;break;default:$payment = $amount;}echo '实际支付: ' , $payment , '<br>';echo '<hr>';// switch:通常用在单值判断中$discount = 0.9;$amount = 13500;// switch ($discount) {// case 0.7:// $payment = $amount * 0.7;// break;// case 0.8:// $payment = $amount * 0.8;// break;// case 0.9:// $payment = $amount * 0.9;// break;// default:// die('不支持的折扣率');// }// 模板语法switch ($discount) :case 0.7:$payment = $amount * 0.7;break;case 0.8:$payment = $amount * 0.8;break;case 0.9:$payment = $amount * 0.9;break;default:die('不支持的折扣率');endswitch;echo '实际支付: ' , $payment , '<br>';echo '<hr>';
error_reporting(E_ALL);$cities = ['合肥', '南京', '杭州', '苏州', '上海'];// 获取当前数组元素的值,先是很笨的办法echo current($cities);next($cities);echo current($cities);next($cities);echo current($cities);next($cities);echo current($cities);next($cities);echo current($cities);next($cities);echo current($cities);// while:进入循环代码之前进行判断,只有条件为真才执行循环体while ($city = current($cities)) {echo $city, '<br>';// 前移指针next($cities);}// 数组指针复位,重新指向第一个reset($cities);while ($city = current($cities)) {echo $city, '<br>';next($cities);}// 模板语法reset($cities);while ($city = current($cities)) :echo $city, '<br>';next($cities);endwhile;
reset($cities);do {echo $city, '<br>';// 循环体内必须要有更新循环条件的语句,否则进入死循环,next($cities);} while ($city = current($cities));// do - while 没有对应的模板语法
// 遍历数组echo count($cities);// $i:数组元素的索引,默认从0开始for ($i = 0; $i < count($cities); $i++) {echo $cities[$i], '<br>';}// 模板语法for ($i = 0; $i < count($cities); $i++) :// 输出拦截, break: 提前终止循环if ($i > 2) break;// 有选择性的输出, 跳过一部分元素if ($i === 3 || $i === 2) continue;echo $cities[$i], '<br>';endfor;
<?php// 数据表查询结果通常是一个二维数组,用二维数组来模拟$users = [['id'=>1, 'name'=>'zhu', 'grade'=> 60],['id'=>2, 'name'=>'admin', 'grade'=> 50],['id'=>3, 'name'=>'peter', 'grade'=> 20],];?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title><?php echo 'Hello' ?></title></head><body><table border="1"><?php for ($i = 0; $i < count($users); $i++) : ?><tr><td><?php echo $users[$i]['id'] ?></td><td><?php echo $users[$i]['name'] ?></td><td><?php echo $users[$i]['grade'] ?></td></tr><?php endfor ?></table></body></html>
$inputnum = 6;if ($inputnum > 4 and $inputnum < 7):echo 'You should go to kindergarden.';elseif ($inputnum > 5 and $inputnum < 11 ):echo 'You could go to primary school.';elseif ($inputnum > 10 and $inputnum < 19):echo 'you could go to high school.';elseif ($inputnum > 18):echo 'you could go to college.';else:echo 'You should go home now.';endif;echo '<hr>';switch ($inputnum) {case '1':echo 'You should go home now.';break;case ($inputnum > 4 and $inputnum < 7):echo 'You should go to kindergarden.';break;case ($inputnum > 18):echo 'you could go to college.';break;case '1':echo 'You should go home now.';break;default:echo 'Good boy!';break;
$schoolName = array('kindergarden', 'primary school', 'high school', 'university');echo $schoolName[0];echo '<hr>';reset($schoolName);while ($school = current($schoolName)) {echo $school, '<br>';next($schoolName);}echo '<hr>';for ($i=0; $i < count($schoolName); $i++) {echo $schoolName[$i], '<br>';}echo '<hr>';for ($i=0; $i < count($schoolName); $i++):echo $schoolName[$i], '<br>';endfor;
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号