PHP逻辑运算符用于对两个或多个表达式进行逻辑运算,返回一个布尔值(true或false)。以下是PHP中常用的逻辑运算符:
以下是逻辑运算符的使用示例:
$a = true;
$b = false;
// 逻辑与
if ($a && $b) {
echo "Both a and b are true";
} else {
echo "Either a or b is false";
}
// 逻辑或
if ($a || $b) {
echo "Either a or b is true";
} else {
echo "Both a and b are false";
}
// 逻辑非
if (!$a) {
echo "a is false";
} else {
echo "a is true";
}
// 逻辑异或
if ($a xor $b) {
echo "Either a or b is true, but not both";
} else {
echo "Both a and b are true or false";
}
输出结果为:
Either a or b is false
Either a or b is true
a is true
Either a or b is true, but not both
上一篇:php显示数组结构
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站