


Why does php ip2long have negative numbers? How to deal with it?
php provides ip2long and long2ip methods for ip address processing.
1. ip2long - Convert an IPV4 string Internet protocol into a digital format
int ip2long ( string $ip_address )
Parameters: ip_address An address in a standard format.
Return value: Returns the converted number of the IP address or FALSE if ip_address is invalid.
2, long2ip - Convert the number format into an IPV4 string Internet protocol
string long2ip ( string $proper_address )
Parameters: proper_address The correct address of the long integer express.
Return value: Returns the Internet address as a string.
3. How to use
$ip = '10.1.1.1'; $ip_long = ip2long($ip); echo $ip_long.PHP_EOL; // 167837953 echo long2ip($ip_long); // 10.1.1.1
4. Reasons for negative numbers and how to deal with them
When the IP address is relatively large, Negative numbers will appear in ip2long:
$ip = '192.168.101.100'; $ip_long = ip2long($ip); echo $ip_long.PHP_EOL; // -1062705820 echo long2ip($ip_long); // 192.168.101.100
Reason explanation:
IPv4 uses unsigned 32-bit addresses, so there are at most 2 to the 32nd power minus 1 (4294967295) addresses. . Write decimal numbers separated by 4 decimal points.
Remember as A.B.C.D, for example: 192.168.100.100.
Each decimal number in the IPv4 address is an unsigned byte, ranging from 0 to 255. Converting the IPv4 address to an unsigned number actually means placing each decimal number in the corresponding 8 bits, forming a 4-byte unsigned integer. 192.168.100.100, 192,168 in the high 8 digits and 100,100 in the low 8 digits.
Solution:
Use %u to format the output as an unsigned integer.
$ip = '192.168.101.100'; $ip_long = sprintf('%u',ip2long($ip)); echo $ip_long.PHP_EOL; // 3232261476 echo long2ip($ip_long); // 192.168.101.100
Related tutorial recommendations: "PHP Tutorial"
The above is the detailed content of Why does php ip2long have negative numbers? How to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
