Logical OR will not work in if statement
this is what i have:
<?php if($row['id']!="9") echo "style=\"display:none\""; ?>
simple enough, it should place style="dsplay:none"
when 'i开发者_运维问答d' is anything but 9. this does the job well, but i wanted to include 'id' 8, 12 and 13 aswell. looked simple enough, just added the logical or like this:
<?php if($row['id']!="8||9||12||13") echo "style=\"display:none\""; ?>
but it does not function anymore, so it places style="dsplay:none"
all the time.
i wanted to go the other way around and did this
<?php if($row['id']=="4||5||6||11") echo "style=\"display:none\""; ?>
but this time around style="dsplay:none"
was never placed.
You want:
if($row['id']!= 8 && $row['id'] != 9 && $row['id'] != 12)
because the current way you are doing it is comparing the value of $row['id']
to the string value "8||9||12||13"
.
Or you can do something like:
if(in_array($row['id'], array(8, 9, 12)))
to condense the condition.
You cannot do it like that.
Either use if($row['id'] == 4 || $row['id'] == 5 || ...)
or use in_array()
: if(in_array($row['id'], array(4,5,6,11)))
I suggest you to write
<?php if(in_array($row['id'], array(8, 9, 12, 13)) /* ... */; ?>
You're comparing numbers with strings in a way that won't work. E.g. an $row['id']
of 1
is not equal to the string "4||5||6||11"
.
You could use the in_array
function which checks whether a value exist in an array:
if(!in_array($row['id'], array(4, 5, 6, 11)) echo ' style="display:none"';
Logical OR's (||
) do not work inside strings.
That's not logical or, that's a string literal. But De Morgan:
if(($row['id']!="8") && $row['id']!="9" && ...
精彩评论