Re: Re: multiple OR's

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux