PHP if语句具有多个“不等于或" [英] PHP if statement with multiple "not equal to or"

查看:980
本文介绍了PHP if语句具有多个“不等于或"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段PHP代码:

So I have this block of PHP code:

<?php
    if ("$page_id" !== '2' || "$page_id" !== '8'){
        $path = './images/'; // Only if $page_id !== '2 || 8'
    } else if ("$page_id" == '5' || "$page_id" == '6' || "$page_id" == '7') {
        $path = '../images/'; // Only if $page_id == '5-7'
    } else {
        $path = '/images/'; // For everything else
    }
?>

但它似乎仅使用($path = './images/';).怎么了?

But it seems to use only ($path = './images/';). What is going wrong?

推荐答案

if ("$page_id" !== '2' || "$page_id" !== '8')

某些事物总是不等于'2'或'8',所以我希望您的意思是:

Something is always unequal to either '2' or '8', so I expect you meant this to be:

if ("$page_id" !== '2' && "$page_id" !== '8')

或者,使其更加清晰(至少与else条件更相符):

Or, to make it more clear (more in line with the else conditions at least):

if (! ("$page_id" == '2' || "$page_id" == '8') )

请注意,==不是严格的,而!==是严格的.适当的对应词是===,因此,如果您希望进行严格的比较,则整个语句将变为:

Note, that == is not strict, while !== is. The proper counterpart would be ===, so if you prefer strict comparisons, the entire statement would become:

if (!("$page_id" === '2' || "$page_id" === '8')){
    $path = './images/'; // Only if $page_id !== '2 || 8'
} else if ("$page_id" === '5' || "$page_id" === '6' || "$page_id" === '7') {
    $path = '../images/'; // Only if $page_id == '5-7'
} else {
    $path = '/images/'; // For everything else
}

但是实际上,最后一个表示对于其他所有东西" ,尽管由于第一个条件(!)中的不等式,第一个感觉像其他所有东西.因此最好将其重写为以下内容(交换路径):

But actually, the last else says 'For everything else', though because of the inequality in the first condition (!) the first one feels like everything else. So it would better be rewritten as follows (paths are swapped):

if ("$page_id" === '2' || "$page_id" === '8'){
    $path = '/images/'; // Only if $page_id === '2 || 8'
} else if ("$page_id" === '5' || "$page_id" === '6' || "$page_id" === '7') {
    $path = '../images/'; // Only if $page_id == '5-7'
} else {
    $path = './images/'; // For everything else
}

这也解决了一个错误,因为正如Nitigya Kuchhal在注释中正确提到的那样,第二个分支将永远不会执行,因为例如5也不是2或8,因此在这种情况下,它将输入-错误- if的第一个分支.

That also solves a bug, because as Nitigya Kuchhal correctly mentioned in the comments, the second branch will never be executed, because for instance 5 will also be not 2 or 8, so in that case it would enter -incorrectly- the first branch of the if.

最后,有些人可能会发现switch语句比类似if else的树更具可读性.它使用了更多行,但是如您所见,您可以更轻松地阅读它,并且几乎可以自我记录.切换并不严格,但是我想在这种情况下这不会成为问题.

And finally, some might find a switch statement more readable than an if else tree like that. It uses more lines, but as you can see, you can read it more easily and it is pretty much self-documenting. Switch isn't strict, though, but I guess that won't be a problem in this case.

还请注意,原始代码中存在的错误(您永远都不会得到5,6或7)已得到修复,实际上,除非您忘记了break,否则实际上没有办法解决该错误.

Also note that the bug that existed in the original code (you would never get 5,6 or 7) is fixed, and there is actually no way to do it wrong now, except when you forget break :).

switch ($page_id)
{
  case '2':
  case '8':
    $path = '/images/';
    break;
  case '5':
  case '6':
  case '7':
    $path = '../images/';
    break;
  default:
    $path = './images/';
    break;
}

这篇关于PHP if语句具有多个“不等于或"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆