PHP逻辑运算符学习资料
PHP逻辑运算符
在PHP中,逻辑运算符用于组合和比较不同的逻辑条件。下面是PHP中常用的逻辑运算符示例:
1. 与运算符(&& 或 and)
$age = 25;
$isStudent = true;
if ($age >= 18 && $isStudent) {
echo "You are eligible for a student discount.";
} else {
echo "Sorry, you are not eligible for a student discount.";
}
2. 或运算符(|| 或 or)
$color = "blue";
$size = "small";
if ($color == "blue" || $size == "small") {
echo "The item is either blue or small.";
} else {
echo "The item is neither blue nor small.";
}
3. 非运算符(!)
$isLogged = false;
if (!$isLogged) {
echo "Please log in to access the content.";
}
4. 逻辑与(and)
$hasPermission = true;
$isMember = true;
if ($hasPermission and $isMember) {
echo "You have permission to access this area.";
} else {
echo "Access denied.";
}
5. 逻辑或(or)
$isAdmin = true;
$isEditor = false;
if ($isAdmin or $isEditor) {
echo "You have access to admin or editor features.";
} else {
echo "Restricted access.";
}