PHP if else 混乱
我正在尝试根据 3 个条件使图像出现和消失,
条件 A = 当用户登录并且其用户名适合显示名称(通过使用 GET 函数)时,它应该回显“是”
条件 B = 当用户登录时已登录,并且它的用户名不适合显示名称,那么它应该回显“否”
条件 C = 当用户未登录时,它也应该回显“否”
我用“是”和“否”交换了图像,以便于参考)
( ,用户有如下设置的 cookie
setcookie("user", $user, $expire);
setcookie("loggedin", 1, $expire);
首先,我获取用户登录时设置的 cookie。
$user1 = $_COOKIE["user"];
$loggedin = $_COOKIE['loggedin'];
$user = strtoupper($user1);
然后我得到了我的球员的名字
$playername = $_GET['player'];
现在我执行了条件
$uplayername = strtoupper($playername);
function showplusicon(){
global $uplayername;
if(($loggedin = "1") and ($user == $uplayername)){
echo "yes";
}
else if (($loggedin = "1") and ($user != $uplayername)){
echo "no";
}
else{
echo "no";
}
}
我不知道有什么问题,但它一直被注册为条件 B。
i'm trying to make an image appearance and disappearance based on 3 condition,
condition A = when user is logged in and it's username fits the displayname(by using the GET function) then it should echo "yes"
condition B = When user is logged in and it's username does not fits the displayname then it should echo "no"
condition C = when user is not logged in then it should echo "no" too
(i swapped the image with yes and no for easier referencing)
By logging in, the user has a cookie which is set like below
setcookie("user", $user, $expire);
setcookie("loggedin", 1, $expire);
First i get the cookie which i set when user logins.
$user1 = $_COOKIE["user"];
$loggedin = $_COOKIE['loggedin'];
$user = strtoupper($user1);
then i get my player's name
$playername = $_GET['player'];
Now i do the conditions
$uplayername = strtoupper($playername);
function showplusicon(){
global $uplayername;
if(($loggedin = "1") and ($user == $uplayername)){
echo "yes";
}
else if (($loggedin = "1") and ($user != $uplayername)){
echo "no";
}
else{
echo "no";
}
}
I don't see what's the problem but it keeps being registered as condition B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
单个等号赋值,而不是比较。
由于你实际上只有两个输出状态,所以你不应该需要 3 个条件;删除条件B。
Single equal signs assign, not compare.
And since you really only have two output states, you shouldn't need 3 conditions; remove condition B.
变量
$loggedin
在您的函数showplusicon()
中未知。您需要将其与global $uplayername
一起添加为global
。由于这已被接受但并不完全完整,因此我将补充一点,正如其他人指出的那样,需要使用
==
相等运算符而不是=
赋值运算符。The variable
$loggedin
isn't known inside your functionshowplusicon()
. You will need to add it as aglobal
along withglobal $uplayername
.Since this was accepted but not totally complete, I'll just add that as others indicated, the
==
equality operator needs to be used instead of the=
assignment operator.当然这应该是:
否则我会 echo $user 和 $uplayername 看看它们是否不同。
Surely this should be:
Otherwise I would echo $user and $uplayername to see if these differ.
首先第一件事是:
$loggedin = "1"
是一个坏主意,因为您实际上是给$loggedin
值“1”而不是进行比较。如果您确定数据类型,请使用==
甚至===
。此外,
$loggedin
在showplusicon()
范围内不可用,因为您没有像使用$ 那样将其声明为全局变量上层玩家名称
。修复上面列出的问题,它应该会工作得更好一些。
First thing's first:
$loggedin = "1"
is a bad idea, as you're actually giving$loggedin
the value "1" instead of comparing. Use==
or even===
if you're sure about the datatype.Further on, the
$loggedin
isn't available in the scope ofshowplusicon()
, as you haven't declared it as a global like you did with$uplayername
.Fix the listed issues above and it should be working a bit better.
如果您在理解自己的代码逻辑时遇到问题,一个简单的方法是将条件分配给自述变量以适应它:
这些变量可以让您轻松地从一开始就调试代码,
从而找到实际的错误:
$loggedin
未定义=
),而不是比较它(==
或= ==)。
然后,您可以另外使用更具可读性的代码流来使您的决定更加明显:
根据您想要输出的内容,甚至可以简化:
希望这对您有帮助。
If you've got problems to understand your own code's logic, a simple way is to assign the conditions to self speaking variables to get used to it:
The variables make it easy to debug your code at the very beginning
so to locate the actual errors:
$loggedin
is undefined=
), not comparing it (==
or===
).You can then use additionally a more readable code-flow to make your decision more visible:
Depending of what you want to output, this can be simplified even:
Hope this is helpful for you.
您的主要问题是变量的全局范围:
Your main problem is todo with global scope of your variables: