Re: reversing an IF statement
| From: | Curt Zirzow | Date: | Sun, 02 May 2004 03:13:02 +0000 |
| Subject: | Re: reversing an IF statement | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185120@lists.php.net to get a copy of this message | ||
* Thus wrote Kim Steinhaug (kim@steinhaug.com):
> Often I end up using a "dumb" IF statement which to me seems that
> it could have been done some other way.
>
> Example :
> if(
> ($_GET["id"]==1) or
> ($_GET["mode"]=="home") or
> ((!isset($_GET["item"])) && ($_GET["mode"]=="news"))
For starters, don't intermatch 'OR' with &&, the have different
precedence's and can lead to unexpected behaviour. The proper 'OR'
that should be used is ||. see: http://php.net/operators
Here is an approach I use to avoid making the if statement real
complicated and kinda makes it more understandable what you are
trying to accomplish with the condition:
$is_ok = $_GET["id"] == 1 ||
$_GET["mode"] == "home" ||
(!isset($_GET["item"]) && $_GET["mode"] == "news"));
if (! is_ok ) {
// yada...
}
Curt
--
"I used to think I was indecisive, but now I'm not so sure."