|| - conditional statement does not evaluate correctly
Moderator: General Moderators
|| - conditional statement does not evaluate correctly
// 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 ?
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 ?
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;
}$IP_num is an integer value, so the problem must be elsewhere, but besides if someone would find it useful:bokehman wrote:Have you actually converted the IP to the integer form. i.e. converted from 000.000.000.000 to 0?
// 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;
Komota: Use the
Code: Select all
tags when posting code!!!- 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
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)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
He's right.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.
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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK