gpt4 book ai didi

php - 为什么我检查整数的 if 条件不起作用?

转载 作者:行者123 更新时间:2023-12-04 10:36:50 25 4
gpt4 key购买 nike

             if($row['rank_code'] == 13 or 14)
{
//admin panel
header("location:home_1");
$_SESSION["hname"] = "$hname";
exit();
}
elseif($row['rank_code'] == 7 or 8 or 9 or 10 or 11 or 12)
{
//senior panel
header("location:home_2");
$_SESSION["hname"] = "$hname";
exit();
}
elseif($row['rank_code'] == 0 or 1 or 2 or 3 or 4 or 5 or 6)
{
//junior panel
header("location:home_3");
$_SESSION["hname"] = "$hname";
exit();
}

因为我想将我的头重定向到 home_3 但它总是将它重定向到 home_1..
因为 $row['rank_code'] 的值来自 mysql 数据库,它是 0..

最佳答案

您可以将 rank_code 值与具有每个 dest 值的数组进行比较:

$sendAdminPanel = array(13,14);
$sendSeniorPanel = array(7,8,9,10,11);
$sendJuniorPanel = array(0,1,2,3,4,5,6);

if (in_array($row['rank_code'], $sendAdminPanel))
{
// go to admin panel
}
elseif(..)
{
// go to senior panel
}
...


或者只是检查 rank_code 是否在每个最大值和最小值之间。此选项的渲染速度比前一个更快:
if($val >= 13 && $val <= 14){
//admin panel
header("location:home_1");
$_SESSION["hname"] = "$hname";
exit();
}elseif($val >= 7 && $val <= 12){
//senior panel
header("location:home_2");
$_SESSION["hname"] = "$hname";
exit();
}elseif($val >= 0 && $val <= 6){
//junior panel
header("location:home_3");
$_SESSION["hname"] = "$hname";
exit();
}else{
echo "rank_code is not valid";
}

您可以在文档中查看 php 比较提示: https://www.php.net/manual/en/language.operators.comparison.php
与许多值进行比较时,请尝试使用数组或其他适合比较值类型的解决方案。

关于php - 为什么我检查整数的 if 条件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60136320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com