PHP if else 混乱

发布于 2024-11-25 14:18:49 字数 933 浏览 1 评论 0原文

我正在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉殇 2024-12-02 14:18:49

单个等号赋值,而不是比较。

if(($loggedin == "1") and ($user == $uplayername)){
   ...

由于你实际上只有两个输出状态,所以你不应该需要 3 个条件;删除条件B。

Single equal signs assign, not compare.

if(($loggedin == "1") and ($user == $uplayername)){
   ...

And since you really only have two output states, you shouldn't need 3 conditions; remove condition B.

拥抱我好吗 2024-12-02 14:18:49

变量 $loggedin 在您的函数 showplusicon() 中未知。您需要将其与 global $uplayername 一起添加为 global

function showplusicon(){

   global $loggedin, $uplayername;

   // etc
}

由于这已被接受但并不完全完整,因此我将补充一点,正如其他人指出的那样,需要使用 == 相等运算符而不是 = 赋值运算符。

if(($loggedin == "1")
             ^^^^

The variable $loggedin isn't known inside your function showplusicon(). You will need to add it as a global along with global $uplayername.

function showplusicon(){

   global $loggedin, $uplayername;

   // etc
}

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.

if(($loggedin == "1")
             ^^^^
缪败 2024-12-02 14:18:49
$loggedin = "1"

当然这应该是:

$loggedin == "1"

否则我会 echo $user 和 $uplayername 看看它们是否不同。

$loggedin = "1"

Surely this should be:

$loggedin == "1"

Otherwise I would echo $user and $uplayername to see if these differ.

我还不会笑 2024-12-02 14:18:49

首先第一件事是:
$loggedin = "1" 是一个坏主意,因为您实际上是给 $loggedin 值“1”而不是进行比较。如果您确定数据类型,请使用 == 甚至 ===

此外,$loggedinshowplusicon() 范围内不可用,因为您没有像使用 $ 那样将其声明为全局变量上层玩家名称

修复上面列出的问题,它应该会工作得更好一些。

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 of showplusicon(), 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.

眼眸印温柔 2024-12-02 14:18:49

如果您在理解自己的代码逻辑时遇到问题,一个简单的方法是将条件分配给自述变量以适应它:

$userIsLoggedIn = $loggedin == "1";
$userIsPlayer = $user == $uplayername;

这些变量可以让您轻松地从一开始就调试代码,

var_dump($userIsLoggedIn, $userIsPlayer);

从而找到实际的错误:

  1. 变量 $loggedin 未定义
  2. if 子句正在设置一个值 (=),而不是比较它(=== ==)。

然后,您可以另外使用更具可读性的代码流来使您的决定更加明显:

if ($userIsLoggedIn) 
{ // user is logged in
    if ($userIsPlayer)
    { // user is player
       ...
    }
    else
    { // user is not player
       ...
    }
} 
else 
{ // user is not logged in
    ...
}

根据您想要输出的内容,甚至可以简化:

if ($userIsLoggedIn && $userIsPlayer)
{
    echo 'yes';
} else
{
    echo 'no';
}

希望这对您有帮助。

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:

$userIsLoggedIn = $loggedin == "1";
$userIsPlayer = $user == $uplayername;

The variables make it easy to debug your code at the very beginning

var_dump($userIsLoggedIn, $userIsPlayer);

so to locate the actual errors:

  1. The variable $loggedin is undefined
  2. The if clauses are setting a value (=), not comparing it (== or ===).

You can then use additionally a more readable code-flow to make your decision more visible:

if ($userIsLoggedIn) 
{ // user is logged in
    if ($userIsPlayer)
    { // user is player
       ...
    }
    else
    { // user is not player
       ...
    }
} 
else 
{ // user is not logged in
    ...
}

Depending of what you want to output, this can be simplified even:

if ($userIsLoggedIn && $userIsPlayer)
{
    echo 'yes';
} else
{
    echo 'no';
}

Hope this is helpful for you.

一影成城 2024-12-02 14:18:49

您的主要问题是变量的全局范围:

<?php 
//Get cookie info
$cookie['user'] = $_COOKIE["user"];
$cookie['loggedin'] = (isset($_COOKIE['loggedin'])&&$_COOKIE['loggedin']=='1')?TRUE:FALSE;

//Set user array
$user['user'] = strtoupper($cookie['user']);
$user['loggedin'] = $cookie['loggedin'];
$user['player'] = $_GET['player'];
$user['uplayername']=strtoupper($user['player']);


function showplusicon(){
    //Made $user array available within function
    global $user;

    if($user['loggedin'] === TRUE && $user['user'] == $user['uplayername']){
        echo "yes";
    }else{
        echo "no";
    }
}
?>

Your main problem is todo with global scope of your variables:

<?php 
//Get cookie info
$cookie['user'] = $_COOKIE["user"];
$cookie['loggedin'] = (isset($_COOKIE['loggedin'])&&$_COOKIE['loggedin']=='1')?TRUE:FALSE;

//Set user array
$user['user'] = strtoupper($cookie['user']);
$user['loggedin'] = $cookie['loggedin'];
$user['player'] = $_GET['player'];
$user['uplayername']=strtoupper($user['player']);


function showplusicon(){
    //Made $user array available within function
    global $user;

    if($user['loggedin'] === TRUE && $user['user'] == $user['uplayername']){
        echo "yes";
    }else{
        echo "no";
    }
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文