if statement with or comparison (newbie)

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-08-2006
JD
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 09-08-2006
Kevin Murphy
 
Posts: n/a
Default 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
>



Reply With Quote
  #3 (permalink)  
Old 09-08-2006
Prathaban Mookiah
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)

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
>
>

Reply With Quote
  #4 (permalink)  
Old 09-08-2006
Mitch Miller
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)

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";
>
> }
>
>

Reply With Quote
  #5 (permalink)  
Old 09-08-2006
Satyam
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)


----- 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
>
>
>

Reply With Quote
  #6 (permalink)  
Old 09-08-2006
Jeremy Privett
 
Posts: n/a
Default RE: [PHP] if statement with or comparison (newbie)

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
>

Reply With Quote
  #7 (permalink)  
Old 09-08-2006
JD
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)

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
Reply With Quote
  #8 (permalink)  
Old 09-08-2006
Robert Cummings
 
Posts: n/a
Default RE: [PHP] if statement with or comparison (newbie)

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. |
`------------------------------------------------------------'
Reply With Quote
  #9 (permalink)  
Old 09-08-2006
tedd
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)

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
Reply With Quote
  #10 (permalink)  
Old 09-09-2006
Robert Cummings
 
Posts: n/a
Default Re: [PHP] if statement with or comparison (newbie)

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. |
`------------------------------------------------------------'
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0