Re: 0.0.0.0 & iplong()

From: Date: Mon, 04 Jun 2012 18:48:32 +0000
Subject: Re: 0.0.0.0 & iplong()
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-318151@lists.php.net to get a copy of this message
On Mon, Jun 4, 2012 at 8:38 PM, jas <jason.gerfen@gmail.com> wrote: > On 06/04/2012 11:33 AM, Matijn Woudt wrote: >> >> On Mon, Jun 4, 2012 at 6:54 PM, jas<jason.gerfen@gmail.com>  wrote: >>> >>> Not sure if this is a bug or not... >>> >>> I have run into an error when performing a conditional using iplong() and >>> the ~ bitwise operator >>> >>> $ip = '0.0.0.0'; >>> $mask = '24'; >>> >>> $end = (ip2long($ip) || (~ip2long($mask))) + 1; >>> >>> PHP Fatal error:  Unsupported operand types >>> >>> I even tried to typecast the mask to (int) >>> >> >> The error is probably not where you suspect it to be. ip2long will >> return false for ip2long($mask), because $mask is not a valid IP >> address. The ~ operator is not supported for false. >> >> - Matijn > > > Strange... this works > > $ip = '10.0.2.0'; > $mask = '24'; > > Anything using 0.0.0.0 fails using the bitmask to calculate valid CIDR > ranges. > Yes, because with $ip being 0.0.0.0, the result of ip2long($ip) will be zero. Zero evaluates to false when PHP is going to parse the || operator, and because of that, it continues to parse the rest. When the result of ip2long($ip) is greater that zero, in case of some other valid IP, it will evaluate to true and PHP will not even execute the rest after the || because the result will be true anyway. Did you mean to use a single | operator, as a bitwise OR? - Matijn

« previous php.general (#318151) next »