Reply to Re: [PHP] Re: multiple OR's

Your name:

Reply:


Posted by Jeff Schmidt on 03/17/05 16:08

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация