A little php help... multiple "if" conditions
I'm not a php coder so... just figuring it out as I go along. I've got a line in one of my Drupal templates that looks for a particular content type then proceeds accordingly
<?php if ($type != 'contenttype1'): ?>
Question: how would I write that to say "if $type is neither contentype1 nor contenttype2"?
I've tried it various ways in the past and just endedup breaking the temlate until I took my php out again.
Apparently I can't just do this?
<?php if ($type != 'specialcontenttype'|'type2'): ?>
Maybe what I really want is more like "if $type is not contentype1 AND is not contentype2"? Conditional logic still confuses me a bit.
pr0gr4mm3r posted this at 22:57 — 27th July 2009.
He has: 1,502 posts
Joined: Sep 2006
Code should be:
<?php
if ($type != 'contenttype1' && $type != 'contenttype2'):
?>
aharown07 posted this at 10:52 — 28th July 2009.
They have: 43 posts
Joined: Oct 2008
Many thanks! That makes sense to me now.
jamespvee posted this at 07:44 — 6th August 2009.
They have: 15 posts
Joined: Aug 2009
Won't that result in the condition having to satisfy both since it's an AND statement?
greg posted this at 11:42 — 6th August 2009.
He has: 1,581 posts
Joined: Nov 2005
Trial and error and testing with the code you're working with to see what it outputs is the best way.
You can simplify your code. If you think:
"&&" is more like "BOTH" (or "ALL" if more than two conditions)
With this:
<?php
if ($type != 'contenttype1' && $type != 'contenttype2'):
?>
Simply means for IF to be TRUE, BOTH conditions have to NOT EQUAL
If your $type variable was set to either "contenttype1" or "contenttype2", then it would return FALSE, as one of the conditions WOULD be equal
I.E. BOTH are "not" NOT EQUAL because one if them IS EQUAL
If however your $type variable was set to "contenttype5", then it would return TRUE, as BOTH conditions are NOT EQUAL to it
You are looking for the var to NOT be BOTH specific things.
------
Whereas using "OR"
"OR" is is more like "EITHER" (or "ANY" if more than two conditions)
The first code you posted:
<?php
if ($type != 'contenttype1' OR $type != 'contenttype2'):
?>
Simply means for IF to be TRUE, EITHER conditions has to NOT EQUAL
This actually would always return TRUE, as $type cannot be both contenttype1 and contenttype2
EG
If $type is set to "contenttype1" then it != "contenttype2" - TRUE
If $type is set to "contenttype2" then it != "contenttype1" - TRUE
With OR you usually use either two different variables to check (or more) or you don't use "does not equal".
EG
<?php
if ($type == 'contenttype1' OR $type == 'contenttype2'):
?>
Simply means for IF to be TRUE, EITHER one of the conditions has to be EQUAL
If your $type variable was set to either "contenttype1" or "contenttype2", then it would return TRUE, as one of the conditions WOULD be equal
If however your $type variable was set to "contenttype5", then it would return FALSE, as NEITHER conditions are EQUAL to it
You are looking for the var to be EITHER of two specific things.
---------
Again, it can get tricky.
If you are doing something that you cannot get your head around, try outputting something simple.
<?php
$type = "test text";
if ($type != 'contenttype1' && $type != 'contenttype2'){
echo "was true";
}else{
echo "was false";
}
?>
aharown07 posted this at 15:31 — 6th August 2009.
They have: 43 posts
Joined: Oct 2008
Thanks. Since I'm using not equal, the && is actually the right way to do it. When you're IFing on "doesn't equal" things get counterintuitive... so in the past I've ended up with an always true condition.
With "&&" (or equivalent) it works because any $type that isn't "contentype1" or "contentype2," will return true for both "doesn't equal" tests.
In short, when you're working "doesn't equal" you achieve "isn't either...or..." by coding "isn't... and isn't..."
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.