This is a discussion on if statement with or comparison (newbie) within the PHP General forums, part of the PHP Programming Forums category; I'm trying to set up a simple conditional, something like this: If my_variable is NOT equal to (black or ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm trying to set up a simple conditional, something like this:
If my_variable is NOT equal to (black or white) echo "wrong color" else echo "right color" Here is what I have tried: if ($_REQUEST['id'] != ("black" or "white")) { echo "wrong color"; } else ( echo "right color"; ) However, no matter what I enter, I always get response "right color". I should add that if I change the if statement to: if ($_REQUEST['id'] != ("black")) then I get "right color" when I enter "black" and "wrong color" for everything else. Would you please point out what's the trivial thing I'm missing here... jd |
|
|||
|
Shouldn't that be this instead:
if (($_REQUEST['id'] != "black") OR ($_REQUEST['id'] != "white")) { echo "wrong color"; } else { echo "right color"; } -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 On Sep 8, 2006, at 2:28 PM, Prathaban Mookiah wrote: > Let me rephrase it. Your color should be black or white to be the > right > colour. Is this correct? > > In that case you should change it to > > if ($_REQUEST['id'] != "black" AND $_REQUEST['id'] != "white") { > > echo "wrong color"; > > } else ( > > echo "right color"; > > } > > ----- Original Message ----- > From: "JD" <jd_borozo@borozo.com> > To: <php-general@lists.php.net> > Sent: Friday, September 08, 2006 5:03 PM > Subject: [php] if statement with or comparison (newbie) > > >> I'm trying to set up a simple conditional, something like this: >> >> If my_variable is NOT equal to (black or white) >> echo "wrong color" >> else >> echo "right color" >> >> Here is what I have tried: >> >> if ($_REQUEST['id'] != ("black" or "white")) { >> >> echo "wrong color"; >> >> } else ( >> >> echo "right color"; >> >> ) >> >> However, no matter what I enter, I always get response "right color". >> >> I should add that if I change the if statement to: >> >> if ($_REQUEST['id'] != ("black")) >> >> then I get "right color" when I enter "black" and "wrong color" for >> everything else. >> >> Would you please point out what's the trivial thing I'm missing >> here... >> >> jd >> >> -- >> 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 > |
|
|||
|
Let me rephrase it. Your color should be black or white to be the right
colour. Is this correct? In that case you should change it to if ($_REQUEST['id'] != "black" AND $_REQUEST['id'] != "white") { echo "wrong color"; } else ( echo "right color"; } ----- Original Message ----- From: "JD" <jd_borozo@borozo.com> To: <php-general@lists.php.net> Sent: Friday, September 08, 2006 5:03 PM Subject: [php] if statement with or comparison (newbie) > I'm trying to set up a simple conditional, something like this: > > If my_variable is NOT equal to (black or white) > echo "wrong color" > else > echo "right color" > > Here is what I have tried: > > if ($_REQUEST['id'] != ("black" or "white")) { > > echo "wrong color"; > > } else ( > > echo "right color"; > > ) > > However, no matter what I enter, I always get response "right color". > > I should add that if I change the if statement to: > > if ($_REQUEST['id'] != ("black")) > > then I get "right color" when I enter "black" and "wrong color" for > everything else. > > Would you please point out what's the trivial thing I'm missing here... > > jd > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > |
|
|||
|
I think the OR should be an AND ...
If $_REQUEST['id'] = "black" then the second test will be true and it will output "wrong color." If the color is "white" then the same thing will happen 'cause it meets the first criteria. -- Mitch Kevin Murphy wrote: > Shouldn't that be this instead: > > > if (($_REQUEST['id'] != "black") OR ($_REQUEST['id'] != "white")) { > > echo "wrong color"; > > } else { > > echo "right color"; > > } > > |
|
|||
|
----- Original Message ----- From: "JD" <jd_borozo@borozo.com> To: <php-general@lists.php.net> Sent: Friday, September 08, 2006 11:03 PM Subject: [php] if statement with or comparison (newbie) > I'm trying to set up a simple conditional, something like this: > > If my_variable is NOT equal to (black or white) > echo "wrong color" > else > echo "right color" > > Here is what I have tried: > > if ($_REQUEST['id'] != ("black" or "white")) { > What PHP (and any parser, for that matter) will try to do is first solve the innermost parenthesis. ("black" or "white"). Many typed languages would give an error at this point, but PHP tries to convert anything it gets to whatever is more useful at the moment. Since both "black" and "white" are not null or empty strings, they are evaluated as true for the logical comparison and true or true is always true. Now, $_REQUEST['id'] might be whatever it is, but dealing with booleans as we are this far, anything but missing or empty string will be true as well, which will give you the second option. Now, first of all, avoid negative comparissons, negative booleans are horrible. Try first to straighten them up or you might get thoroughly confussed: if ($_REQUEST['id] == 'black' or $_REQUEST['id'] == 'white') { echo 'right color'; } else { echo 'wrong color''; } And so as you know why it is good to straighten negative booleans, this would be the twisted way if ($_REQUEST['id] != 'black' and $_REQUEST['id'] != 'white') { echo 'wrong color''; } else { echo 'right color'; } Notice that not only the comparisson changed but now they are joined by an AND instead of an OR and the then and else parts are swapped. Satyam > echo "wrong color"; > > } else ( > > echo "right color"; > > ) > > However, no matter what I enter, I always get response "right color". > > I should add that if I change the if statement to: > > if ($_REQUEST['id'] != ("black")) > > then I get "right color" when I enter "black" and "wrong color" for > everything else. > > Would you please point out what's the trivial thing I'm missing here... > > jd > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > |
|
|||
|
Well, it could be this, too:
switch( $_REQUEST['id'] ) { case "white": echo "Right color."; break; case "black": echo "Right color."; break; default: echo "Wrong color."; break; } --- Jeremy C. Privett Director of Product Development Zend Certified Engineer Completely Unique jprivett@completelyunique.com Phone: 303.459.4819 Fax: 303.459.4821 Web: www.completelyunique.com This email may contain confidential and privileged material for the sole use of the intended recipient. Any review or distribution by others is strictly prohibited. If you are not the intended recipient please contact the sender and delete all copies. Your compliance is appreciated. -----Original Message----- From: Kevin Murphy [mailto:php@stubborndonkey.com] Sent: Friday, September 08, 2006 3:26 PM To: php Cc: JD Subject: Re: [php] if statement with or comparison (newbie) Shouldn't that be this instead: if (($_REQUEST['id'] != "black") OR ($_REQUEST['id'] != "white")) { echo "wrong color"; } else { echo "right color"; } -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 On Sep 8, 2006, at 2:28 PM, Prathaban Mookiah wrote: > Let me rephrase it. Your color should be black or white to be the > right > colour. Is this correct? > > In that case you should change it to > > if ($_REQUEST['id'] != "black" AND $_REQUEST['id'] != "white") { > > echo "wrong color"; > > } else ( > > echo "right color"; > > } > > ----- Original Message ----- > From: "JD" <jd_borozo@borozo.com> > To: <php-general@lists.php.net> > Sent: Friday, September 08, 2006 5:03 PM > Subject: [php] if statement with or comparison (newbie) > > >> I'm trying to set up a simple conditional, something like this: >> >> If my_variable is NOT equal to (black or white) >> echo "wrong color" >> else >> echo "right color" >> >> Here is what I have tried: >> >> if ($_REQUEST['id'] != ("black" or "white")) { >> >> echo "wrong color"; >> >> } else ( >> >> echo "right color"; >> >> ) >> >> However, no matter what I enter, I always get response "right color". >> >> I should add that if I change the if statement to: >> >> if ($_REQUEST['id'] != ("black")) >> >> then I get "right color" when I enter "black" and "wrong color" for >> everything else. >> >> Would you please point out what's the trivial thing I'm missing >> here... >> >> jd >> >> -- >> 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 > |
|
|||
|
At 05:30 PM 9/8/2006, you wrote:
>----- Original Message ----- From: "JD" <jd_borozo@borozo.com> >To: <php-general@lists.php.net> >Sent: Friday, September 08, 2006 11:03 PM >Subject: [php] if statement with or comparison (newbie) > > >>I'm trying to set up a simple conditional, something like this: >> >>If my_variable is NOT equal to (black or white) >> echo "wrong color" >>else >> echo "right color" >> >>Here is what I have tried: >> >> if ($_REQUEST['id'] != ("black" or "white")) { > >What PHP (and any parser, for that matter) will try to do is first solve >the innermost parenthesis. ("black" or "white"). Many typed languages >would give an error at this point, but PHP tries to convert anything it >gets to whatever is more useful at the moment. Since both "black" and >"white" are not null or empty strings, they are evaluated as true for the >logical comparison and true or true is always true. Now, $_REQUEST['id'] >might be whatever it is, but dealing with booleans as we are this far, >anything but missing or empty string will be true as well, which will give >you the second option. > > >Now, first of all, avoid negative comparissons, negative booleans are >horrible. Try first to straighten them up or you might get thoroughly >confussed: > > >if ($_REQUEST['id] == 'black' or $_REQUEST['id'] == 'white') { > echo 'right color'; >} else { >echo 'wrong color''; >} > >And so as you know why it is good to straighten negative booleans, this >would be the twisted way > >if ($_REQUEST['id] != 'black' and $_REQUEST['id'] != 'white') { >echo 'wrong color''; >} else { > echo 'right color'; >} > >Notice that not only the comparisson changed but now they are joined by an >AND instead of an OR and the then and else parts are swapped. > >Satyam Thank you all! jd |
|
|||
|
On Fri, 2006-09-08 at 15:30 -0600, Jeremy Privett wrote:
> Well, it could be this, too: > > switch( $_REQUEST['id'] ) { > case "white": > echo "Right color."; > break; > > case "black": > echo "Right color."; > break; > > default: > echo "Wrong color."; > break; > } Ugh, if you're going to use a big ugly case statement for something so trivial at least make use of the fall-through feature: <?php switch( $_REQUEST['id'] ) { case 'white': case 'black': { echo 'Right color.'; break; } default: { echo 'Wrong color.'; } } ?> Cheers, Rob. -- ..------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' |
|
|||
|
At 5:03 PM -0400 9/8/06, JD wrote:
>I'm trying to set up a simple conditional, something like this: > >Here is what I have tried: > > if ($_REQUEST['id'] != ("black" or "white")) { In all of the answers given thus far, no one mentioned that the use of $_REQUEST has a security issue with regard to where the $_REQUEST originated. $_REQUEST is an array consisting of $_GET, $_POST and $_COOKIE values and as such, you don't know where the data came from and that might be important. So, wouldn't it be better to recommend that the poster use $_GET, $_POST, or $_COOKIE instead of $_REQUEST? Just an idea -- comments? tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|||
|
On Fri, 2006-09-08 at 18:38 -0400, tedd wrote:
> At 5:03 PM -0400 9/8/06, JD wrote: > >I'm trying to set up a simple conditional, something like this: > > > >Here is what I have tried: > > > > if ($_REQUEST['id'] != ("black" or "white")) { > > > In all of the answers given thus far, no one mentioned that the use > of $_REQUEST has a security issue with regard to where the $_REQUEST > originated. > > $_REQUEST is an array consisting of $_GET, $_POST and $_COOKIE values > and as such, you don't know where the data came from and that might > be important. > > So, wouldn't it be better to recommend that the poster use $_GET, > $_POST, or $_COOKIE instead of $_REQUEST? Nope, not inherently less secure. If you are properly cleaning and validating your data (as every good program should) then it doesn't matter whether you pull from $_GET, $_POST, or $_REQUEST. The only time it's bad is if you make assumptions about the value received -- AND YOU SHOULD NEVER ASSUME YOU HAVE CLEAN DATA FROM AN OUTSIDE SOURCE!! Cheers, Rob. -- ..------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' |
![]() |
| Thread Tools | |
| Display Modes | |
|
|