IF子句仅给出最后IF条件的值

时间:2013-09-04 19:58:21

标签: php if-statement

我有以下IF条件,但总是给我最后的$ genreid 6000

if ($URL == "/apps.html"
    or "/apps/all-genres.html"
    or "/apps/all-genres/top-paid-apps.html"
    or "/apps/all-genres/top-free-apps.html"
) {
    $genreid = "36";
}

if ($URL == "/apps/business.html"
    or "/apps/business/top-paid-apps.html"
    or "/apps/business/top-free-apps.html"
) {
    $genreid = "6000";
}

有人可以帮我纠正吗?

3 个答案:

答案 0 :(得分:7)

你的所有人都搞砸了。 OR“foo”将解决为真。

尝试这样的事情:

if (in_array($URL,array('test1','test2','test3') ))
{
    $genreid = "36";
}

答案 1 :(得分:4)

非空(和非“0”)字符串的计算结果为“true”。您的代码看起来应该更像这样:

if ($URL == "/apps.html"
    or $URL == "/apps/all-genres.html"
    or $URL == "/apps/all-genres/top-paid-apps.html"
    or $URL == "/apps/all-genres/top-free-apps.html") {
        $genreid = "36";
}

答案 2 :(得分:2)

if ($URL == "/apps/business.html"
            or "/apps/business/top-paid-apps.html"
            or "/apps/business/top-free-apps.html"
            )

这将检查以下其中一项是否真实

  • $URL"/apps/business.html"
  • "/apps/business/top-paid-apps.html"
  • "/apps/business/top-free-apps.html"

这两个字符串是真的,这不是or运算符的工作方式。依次将$URL与每个进行比较,或使用数组:

if(in_array($URL, ['/apps/business.html', …])) {
    …
}

但更不用说 - 为什么你不首先使用elseif