php:复合if语句(被忽略) [英] php: compound if statement (is being ignored)

查看:82
本文介绍了php:复合if语句(被忽略)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果子域不是移动并且文件名不是设计或照片,我想回应一些东西,所以echo if(不是mobile而不是design)或(不是mobile)而不是照片)。

I want to echo something if the subdomain is not "mobile" and the file name is not "design" or "photo", so echo if (not "mobile" and not "design") or (not "mobile" and not "photo").

我写的代码是:

<?php
    if ( ($subdomain != mobile) && ($currentFileName != design || $currentFileName != photo) ) {
        echo ('code')
    }
?>

但它不喜欢复合语句。如果我只是($ subdomain!= mobile)它有效;如果我只是($ currentFileName!= design)它有效;如果我只是($ currentFileName!= photo)它可以工作......
上面的代码不会导致错误......它只是没有做任何事情。

but it doesn't like the compound statement. if i just do ($subdomain!=mobile) it works; if i just do ($currentFileName!=design) it works; and if i just do ($currentFileName!=photo) it works…. The above code doesn't result in an error…it just doesn't do anything.

是否有一种特殊的方法来复合/分组语句我只是没有得到?我觉得这应该不难(而且我用PHP做了更复杂的事情,没有这么多麻烦......)。
我在此之后模仿了我的陈述: http:// www.php.net/manual/en/control-structures.if.php#101724

Is there a particular way to compound/group the statement that I'm just not getting? I feel like this shouldn't be difficult (and I've done much more complex things with php without this much trouble…). I modelled my statement after this: http://www.php.net/manual/en/control-structures.if.php#101724

谢谢!

编辑:
fyi:

fyi:

$subdomain = implode(array_slice(array_reverse(explode('.', $baseURL)),2)); //resolves to "test"
$basename = basename($_SERVER['REQUEST_URI']); //resolves to "design.php"
$currentFileName = basename($basename, ".php"); //resolves to "design"

新问题是子域名mobile中的所有内容现在都是被捕,所以没有任何页面从echo获得代码(即使不是'design'或'photo')。

the new problem is that everything in the subdomain "mobile" is now being caught, so no pages in it are getting the "code" from echo (even the ones not 'design' or 'photo').

最近的代码我尝试过:

if ( ($subdomain != 'test' && $currentFileName != 'design') && ($subdomain!='test' && $currentFileName != 'photo') ) {
    echo ('ajax code');
}

但仍然没有骰子:(

推荐答案

你的逻辑存在缺陷。你很可能想要&& 而不是 || 。如果你想一下这句话就会显而易见。让我们说下面的场景:$ currentFileName是'design'。它会通过,因为它不是'photo'。如果我们做的话$ currentFileName等于'photo'它仍然会通过,因为它不等于'design'。

There's a flaw in your logic. You most likely want && not ||. If you think about the statement for a minute it will become apparent. Let's say the following scenario: $currentFileName is 'design'. It will pass since it's not 'photo'. If we make $currentFileName equal 'photo' it will still pass since it doesn't equal 'design'.

编辑:
并且在比较字符串时请使用引号.PHP将首先尝试使用该名称查找常量,然后假设它是一个字符串,因为它找不到一个。

And PLEASE use quotes when comparing strings. PHP will attempt to find a constant with that name first, then assume it's a string because it doesn't find one.

这篇关于php:复合if语句(被忽略)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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