Question

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 hereIP 1.0.0.1 and i am getting current output 16777217

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top