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

Discuss the solution to the problem of too large a negative number when the PHP function ip2long converts IP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:08:281016browse

【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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327463.htmlTechArticle【Cause】: Because PHP's integer type is signed, and many IP addresses will result in negative integers. 【Solution Method]: Its official manual mentions that "you need...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn