Re: Re: multiple OR's
| From: | Jeff Schmidt | Date: | Thu, 17 Mar 2005 14:08:01 +0000 |
| Subject: | Re: Re: multiple OR's | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-210978@lists.php.net to get a copy of this message | ||
AndreaD wrote:
This works...But not the way you think it does.
if ($name == jim || andrea || tommy)This if statement will first check to see if $name == "jim" (oh yeah, you are missing quotes around jim, andrea, and tommy, by the way). Then, it does *NOT* check to see if $name == andrea. Instead, what it does, is it treats "andrea" as a seperate test condition. A simple value by itself, with no logical operator, gets automatically cast to a boolean value. Because the string "Andrea" is not empty ("") or "0", PHP casts it as TRUE. (See http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting for more details). So, this conditional is always true (assuming andrea and tommy are wrapped in string delimters so that they are strings; it could be possible that you have defined constants called jim, andrea, and tommy, in which case you don't need the quotes - in any case, the same basic principle applies - the == operator only applies to jim). Even if $name = 'Bob', your conditional will evaluate to TRUE, and your code block will execute. I suspect this is not the behavior you want. As an earlier poster suggested, the best way to do this would be to use the in_array construct, which tests to see if the first value is in the array specified as the second value. if (in_array($name, array("jim", "andrea", "bob"))) { //code here } Jeff Schmidt