Page 1 of 1
|| - conditional statement does not evaluate correctly
Posted: Sat Jul 01, 2006 4:11 am
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 ?
Posted: Sat Jul 01, 2006 4:17 am
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;
}
Posted: Sat Jul 01, 2006 4:36 am
by bokehman
Have you actually converted the IP to the integer form. i.e. converted from 000.000.000.000 to 0?
Posted: Sat Jul 01, 2006 4:50 am
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;
Posted: Sat Jul 01, 2006 5:18 am
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().
Posted: Sat Jul 01, 2006 7:04 am
by JayBird
Re: || - conditional statement does not evaluate correctly
Posted: Sat Jul 01, 2006 11:24 am
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;
}
Posted: Sat Jul 01, 2006 6:53 pm
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.
Posted: Sat Jul 01, 2006 7:11 pm
by AKA Panama Jack
GRRRRRR!
(Sorry I shouldn't have as it was offtopic but the user icon above just made it pop out.)
Posted: Sat Jul 01, 2006 7:14 pm
by Ollie Saunders
YES
its gir btw because he was originally a sir but he got confused