-3

我有以下 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";
}

有人可以帮我纠正这个吗?

4

3 回答 3

7

你的ORS都搞砸了。OR "foo" 将解析为 true。

尝试这样的事情:

if (in_array($URL,array('test1','test2','test3') ))
{
    $genreid = "36";
}
于 2013-09-04T20:01:33.100 回答
4

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

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";
}
于 2013-09-04T20:01:44.313 回答
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

于 2013-09-04T20:02:20.460 回答