我无法比较 PHP 中的两个字符串

发布于 2024-10-21 21:37:00 字数 225 浏览 2 评论 0原文

    <?php
    $gender = "devilcode";
    if (($gender == "female") || ($gender = "male"))
        {
            echo "ok";
        }
    else echo "no";
    ?>

它应该输出“no”,但它输出“ok”。我做错了什么?

    <?php
    $gender = "devilcode";
    if (($gender == "female") || ($gender = "male"))
        {
            echo "ok";
        }
    else echo "no";
    ?>

It should output "no" but it outputs "ok". What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

软糯酥胸 2024-10-28 21:37:00

您将 $gender 指定为男性,而不是测试它进行比较,您需要两个等号:

$gender = "devilcode";
if (($gender == "female") || ($gender == "male"))
    echo "ok";
else 
    echo "no";

You are assigning $gender to be male, rather than testing it for comparison, you need two equal signs:

$gender = "devilcode";
if (($gender == "female") || ($gender == "male"))
    echo "ok";
else 
    echo "no";
—━☆沉默づ 2024-10-28 21:37:00

你缺少一个等号:

($gender == "male")

编辑:正是因为这个原因,许多程序员建议以相反的方式编写比较,将静态值放在左侧:

if ("male" == $gender) { ... }

这样,如果你忘记并使用 = 而不是 ==,你会得到语法错误。

You're missing an equals sign:

($gender == "male")

Edit: For this very reason, many coders suggest writing the comparison the other way around, with the static value on the left:

if ("male" == $gender) { ... }

This way, if you forget and use = instead of ==, you get a syntax error.

樱花坊 2024-10-28 21:37:00

IF 的第二部分是 $gender="male" 吗?我认为这总是返回 true 并且导致了问题。设为$gender=="male"

Is the second part of the IF $gender="male"? I think this is returning true always and is causing the problem. Make it $gender=="male"

情绪少女 2024-10-28 21:37:00

您的测试的第二部分存在错误。替换

($gender = "male") // assign $gender with the value "male"
                   // this is always evaluated to TRUE in your test

($gender == "male") // test that $gender is equal to "male"

there is a bug in the second part of your test. replace

($gender = "male") // assign $gender with the value "male"
                   // this is always evaluated to TRUE in your test

by

($gender == "male") // test that $gender is equal to "male"
吃颗糖壮壮胆 2024-10-28 21:37:00

编写条件的良好做法是使用反向表示法,以防止出现此类错误:

if ('female' === $gender || 'male' === $gender) {...    

或:

$allowed = array('male', 'female', 'unknown');
if (in_array($gender, $allowed)) {... 

Good practice for writing conditions, which protects from errors like this is using inverse notation:

if ('female' === $gender || 'male' === $gender) {...    

or:

$allowed = array('male', 'female', 'unknown');
if (in_array($gender, $allowed)) {... 
独守阴晴ぅ圆缺 2024-10-28 21:37:00

您将 var $gender 分配给 'male' ($gender = 'male'),而不是 comaring ($gender == 'male') code>)

另外,如果您想测试几种可能的结果,请尝试 in_array()

$gender = 'devilcode'
$genders = array('female','male','unknown')
if(in_array($gender, $genders)){
 echo 'ok';
}else{
 echo 'no';
}

问候。

You are assigning the var $gender to 'male' ($gender = 'male') instead of copmaring it ($gender == 'male')

Also if you want to test against several possible outcomes try in_array()

$gender = 'devilcode'
$genders = array('female','male','unknown')
if(in_array($gender, $genders)){
 echo 'ok';
}else{
 echo 'no';
}

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文