Home  >  Article  >  Backend Development  >  PHP function ip2long() implements a solution to the problem of negative numbers when converting IP to integer

PHP function ip2long() implements a solution to the problem of negative numbers when converting IP to integer

WBOY
WBOYOriginal
2016-07-25 08:57:401559browse
  1. $ip = "192.168.1.2";
  2. $ip_n = ip2long($ip);
  3. echo $ip_n; //Get -1062731518
  4. ?>
Copy code

Reason: The integer value converted by IP is too large and exceeds the range of the integer, so it becomes a negative number.

You need to make the following modifications, modify it to $ip_n = bindec(decbin(ip2long($ip))) to get the unsigned integer.

For example:

  1. $ip = "192.168.1.2";
  2. $ip_n = bindec(decbin(ip2long($ip)));
  3. //by bbs.it-home.org
  4. echo $ ip_n; //Get 3232235778
  5. ?>
Copy code


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