Reputation: 33
The following code works fine on http://writecodeonline.com/php/
but unfortunately it cannot provide current output at my localhost.
$eip = '255.255.255.254';
echo $longeip = ip2long($eip);
Output should be displayed like this
4294967294
but it gives 2
For enter code here
IP 1.0.0.1 and i am getting current output 16777217
Upvotes: 0
Views: 151
Reputation: 37365
That's because ip2long()
will return integer value which is signed in PHP, so available inteval is [-1*2^31 .. 2^31-1]
. Thus, it will be -2
because of binary representation.
If you want unsigned value, use
$longeip = sprintf('%u', ip2long($eip));
here %u
specifies "unsigned integer" for sprintf()
. Be aware that result will be string in this case.
Upvotes: 1