php: compound if statement (is being ignored)
I want to echo something if the subdomain is not "mobil开发者_JAVA百科e" and the file name is not "design" or "photo", so echo if (not "mobile" and not "design") or (not "mobile" and not "photo").
The code I wrote is:
<?php
if ( ($subdomain != mobile) && ($currentFileName != design || $currentFileName != photo) ) {
echo ('code')
}
?>
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.
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
Thanks!
EDIT: 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"
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').
the most recent code I tried was:
if ( ($subdomain != 'test' && $currentFileName != 'design') && ($subdomain!='test' && $currentFileName != 'photo') ) {
echo ('ajax code');
}
but still no dice :(
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'.
EDIT: 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.
Replace ||
to &&
- there should be 'AND'
if ( ($subdomain != 'mobile') && ($currentFileName != 'design' && $currentFileName != 'photo') ) {
echo ('code');
}
I understand it should be:
<?php
if ($subdomain != 'mobile' && $currentFileName != 'design' && $currentFileName != 'photo') {
echo ('code');
}
?>
Because the 'or' make it to match any file name, including 'design' and 'photo'.
Good point, quotes added :)
If you're comparing strings, you should quote them. Otherwise, it thinks they are constants.
Try:
<?php
if ( ($subdomain != 'mobile') && ($currentFileName != 'design' || $currentFileName != 'photo') ) {
echo 'code';
}
?>
If that still doesn't work as expected, debug $subdomain
and $currentFileName
to ensure they are what you think.
Note: per the comments it would seem that you're logic is incorrect as well.
精彩评论