This paper discusses the solution to the problem of the negative value when the PHP function ip2long transforms IP

  • 2020-06-07 04:06:05
  • OfStack

[Causes] : Because PHP integer is signed, and many IP addresses result in integers integer is signed, and many IP addresses in negative integers
Solution: According to the official manual, it can be used as follows: "you need to use the "%u" formatter sprintf() or printf() to get string string unsigned IP address"
printf('%u', ip2long('IP address '));
Or convert first to base 2 and then to base 10, bindec(decbin(ip2long('IP address '));
$strIp = '';

echo ip2long ($strIp); // The output at this time is -1233780736
echo ' < br/ > ';
echo bindec(decbin(ip2long($strIp))); Output 3061186560, and MySQL function output 1 ~

【 note 】 :
number bindec (string $binary_string); Convert from base 2 to base 10
string decbin (int $number); // Convert from base 10 to base 2

Related articles: