|| - conditional statement does not evaluate correctly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Komota
Forum Newbie
Posts: 3
Joined: Sat Jul 01, 2006 2:26 am

|| - conditional statement does not evaluate correctly

Post by Komota »

// excluding our IP and localhost from posting a visit
if (($IP_num!=2130706433) || ($IP_num!=3579574224))
{
some code block;
}

It's weird, since i am pretty sure $IP_num equals the first value, the whole || - conditional statement returns TRUE, where it should actually return FALSE for the code block not to be executed.

While this works:
if ($IP_num!=2130706433) - returns FALSE
{
some code block;
}
Tried also using 'or' - same result.
Anyone suggest a solution ?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

If one of your strings is being intepreted as a integer rather than a string and there is a 0 in front of it that could cause a problem unless you encapsulate them in quotes.

Code: Select all

// excluding our IP and localhost from posting a visit
if (($IP_num != '2130706433') || ($IP_num != '3579574224')) {
 //some code block;
}

if ($IP_num != '2130706433') { //- returns FALSE
  //some code block;
}
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Have you actually converted the IP to the integer form. i.e. converted from 000.000.000.000 to 0?
Komota
Forum Newbie
Posts: 3
Joined: Sat Jul 01, 2006 2:26 am

Post by Komota »

bokehman wrote:Have you actually converted the IP to the integer form. i.e. converted from 000.000.000.000 to 0?
$IP_num is an integer value, so the problem must be elsewhere, but besides if someone would find it useful:

// Getting user IP
$User_IP = getenv('REMOTE_ADDR');
// Splitting user IP
$w = strtok($User_IP,'.');
$x = strtok('.');
$y = strtok('.');
$z = strtok('.');
// calculate IP-number from separate parts
$IP_num=16777216*$w+65536*$x+256*$y+$z;
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Komota wrote:$IP_num is an integer value, so the problem must be elsewhere, but besides if someone would find it useful:
There's a built in function for that: ip2long().
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Komota: Use the

Code: Select all

tags when posting code!!!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: || - conditional statement does not evaluate correctly

Post by Christopher »

It should be && not ||. This is because you are using != which is inverting your logic. You want it not be NOT BOTH because that is everything else.

Code: Select all

// excluding our IP and localhost from posting a visit
    if (($IP_num!=2130706433) && ($IP_num!=3579574224)) 
           {
               some code block;
           }
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

It should be && not ||. This is because you are using != which is inverting your logic. You want it not be NOT BOTH because that is everything else.
He's right.

Everyone should have known that a subject like this: "|| - conditional statement does not evaluate correctly" means the logic is incorrect. No language gets logic wrong.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

GRRRRRR!

(Sorry I shouldn't have as it was offtopic but the user icon above just made it pop out.)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

YES :)

its gir btw because he was originally a sir but he got confused
Post Reply