Home >Backend Development >PHP Tutorial >Discuss the solution to the problem of too large a negative number when the PHP function ip2long converts IP_PHP Tutorial
【Cause】: Because PHP's integer type is signed, and many IP addresses will result in negative integers.
【Solution】: Its official manual mentions that "you need to use the "%u " formatter of sprintf() or printf() to get the string representation of the unsigned IP address"
i.e., printf( '%u' , ip2long( 'IP address' ) );
Or convert it to binary first and then convert it to decimal, bindec( decbin( ip2long( 'IP address' ) ) );
[Test]
$strIp = '182.118.0.0';
echo ip2long($strIp); //-1233780736 output at this time
echo '
';
echo bindec( decbin( ip2long( $strIp ) ) ); // Output 3061186560, consistent with the MySQL function output~
[Note]:
number bindec ( string $binary_string ); //Convert binary to decimal
string decbin ( int $number ); // Convert decimal to binary