批改状态:合格
老师批语:有自己的想法,前端给的数据不一定可靠,自己验证一遍才是真的放心
比较运算符就是用运算符将左右两侧的值进行比较,它只能返回两种结果:true 或 false
逻辑运算符
两者经常会结合在一起在 if 函数中使用
$a = 10;$b = 5;$c = 10;$d = "10";// 比较运算符if ($a < $b) {echo "正确1";} else if ($a <= $b) {echo "正确2";} else if ($b > $c) {echo "正确3";} else if ($b >= $c) {echo "正确4";} else if ($a === $d) {echo "正确5";} else if ($a !== $c) {echo "正确6";} else {echo "非法判断";}echo "<hr>";// 比较运算符与逻辑运算符// &&// true&&falseif ($a > $b && $a > $c) {echo "正确1";}// true&&trueif ($a > $b && $a === $c) {echo "正确1";}// false&&falseif ($a < $b && $a !== $c) {echo "正确1";}// ||// true||falseif ($a > $b || $a > $c) {echo "正确1";}// true||trueif ($a > $b || $a === $c) {echo "正确1";}// false||alseif ($a < $b || $a !== $c) {echo "正确1";}// xor// true xor falseif ($a > $b xor $a > $c) {echo "正确1";}// true xor trueif ($a > $b xor $a === $c) {echo "正确1";}// false xor alseif ($a < $b xor $a !== $c) {echo "正确1";}// !// !trueif (!($a < $b)) {echo "正确";}// !falseif (!($a > $b)) {echo "正确";}
常用的逻辑函数有
switch 函数以 switch 关键词开头 后面跟小括号,小括号内部是传入需要判断的参数,再后面跟大括号,里面是需要执行的判断代码,每一个判断代码是 case 后面加判断条件,以:输出结果,如果判断到是哪个 case 执行,执行完毕后需要加一个 break 终止代码,否则会一直执行,最后可以加一个 default 来处理非上面判断内容的情况(类似于 if 语句中最后的 else)
用 switch 制作一个简易计算器
<?php$notice = "";@$num1 = (int)$_GET["num1"];@$num2 = (int)$_GET["num2"];@$opt = $_GET["opt"];//判断内容为空提示请输入内容if (@$_GET["num1"] === "" && @$_GET["num2"] === "") {$notice = '<span style="color:red">结果:请输入内容</span>';//限定输入的两个值最大1000亿} else if (strlen($num1) > 12 && strlen($num1) > 12) {$notice = '<span style="color:red">结果:ops~~~ 输入的数值忒长,算不过来啦</span>';//限定除法跟取余第二个值不能为0} else if (($opt === "÷" || $opt === "%") && $num2 === 0) {$notice = '<span style="color:red">结果:除数不能为0哦~</span>';//判断输入内容在地址栏被篡改后给出提示} else if ((@$_GET["num1"] != "" && $num1 === 0) || (@$_GET["num2"] != "" && $num2 === 0)) {$notice = '<span style="color:red">结果:请输入合法数值~</span>';//判断运算符在地址栏被篡改后给出提示} else if (@$_GET["num1"] != "" && @$_GET["num2"] != "" && $opt = "") {$notice = '<span style="color:red">结果:请选择正确的运算符~</span>';} else {// switch进行运算switch ($opt) {case "+":$notice = $num1 + $num2;break;case "-":$notice = $num1 - $num2;break;case "×":$notice = $num1 * $num2;break;case "÷":$notice = $num1 / $num2;break;case "%":$notice = $num1 % $num2;break;}}?><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>简易计算器</title></head><body><h2>计算器</h2><form action="" method="GET"><!-- 如果值不为空且输入过保留输入过的值 --><input type="number" name="num1" value=<?php if (@$_GET["num1"] != "") echo $num1 ?> required><select name="opt" id="opt"><!-- 判断上次选择的哪个运算符保留该值 --><option value="+" <?php if ($opt === "+") echo "selected" ?>>+</option><option value="-" <?php if ($opt === "-") echo "selected" ?>>-</option><option value="×" <?php if ($opt === "×") echo "selected" ?>>×</option><option value="÷" <?php if ($opt === "÷") echo "selected" ?>>÷</option><option value="%" <?php if ($opt === "%") echo "selected" ?>>取余</option></select><!-- 如果值不为空且输入过保留输入过的值 --><input type="number" name="num2" value=<?php if (@$_GET["num2"] != "") echo $num2 ?> required><button>计算</button></form><!-- 输出值 --><?php echo $notice; ?></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号