[PHP] reversing an IF statement
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)) ) { // Here we do nothing } else { // This is where we do it } If we translate the above to simpler reading we could say : if(statement) // skip else // Do the stuff I'm ofcourse looking for this if(!statement) // Do the stuff Problem is, when using more statements I never seem to find the way of doing it without having an empty {} in it, dont know if you see my problem here however, its the best I can exmplain. For all I know it has to be like this. -- -- Kim Steinhaug -- There are 10 types of people when it comes to binary numbers: those who understand them, and those who don't. -- www.steinhaug.com - www.easywebshop.no - www.webkitpro.com -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] reversing an IF statement
The easiest method is just to encase all the checks in parenthese, then negate it: if(!( ($_GET[id]==1) or ($_GET[mode]==home) or ((!isset($_GET[item])) ($_GET[mode]==news)) )) { // This is where we do it } -Original Message- From: Kim Steinhaug [mailto:[EMAIL PROTECTED] Sent: Saturday, May 01, 2004 3:29 AM To: [EMAIL PROTECTED] Subject: [PHP] reversing an IF statement 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)) ) { // Here we do nothing } else { // This is where we do it } If we translate the above to simpler reading we could say : if(statement) // skip else // Do the stuff I'm ofcourse looking for this if(!statement) // Do the stuff Problem is, when using more statements I never seem to find the way of doing it without having an empty {} in it, dont know if you see my problem here however, its the best I can exmplain. For all I know it has to be like this. -- -- Kim Steinhaug -- There are 10 types of people when it comes to binary numbers: those who understand them, and those who don't. -- www.steinhaug.com - www.easywebshop.no - www.webkitpro.com -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] reversing an IF statement
* Thus wrote Kim Steinhaug ([EMAIL PROTECTED]): 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php