PHP long2ip() Function: Everything You Need to Know
As a PHP developer, you may need to convert a long integer representing an IP address to a human-readable format. The long2ip() function is a built-in function
An IPv4 address such as 127.0.0.1 is, under the hood, just a 32-bit number. Databases and binary protocols often store addresses in that compact integer form because comparing and indexing an integer is faster and smaller than comparing a string. When you read such a value back, you need to turn it into the familiar dotted-quad notation that humans recognize. PHP's built-in long2ip() function does exactly that conversion.
This chapter explains what the function does, how integers map to IPv4 addresses, the signed/unsigned gotcha you will eventually hit, and where the function is genuinely useful.
What is the long2ip() Function?
long2ip() converts a 32-bit integer that encodes an IPv4 address into its human-readable dotted-quad string (for example 192.168.0.1).
A dotted-quad is four 8-bit numbers (each 0–255) joined by dots. Because each octet is one byte, the whole address fits in 4 bytes = 32 bits. long2ip() simply splits that integer back into four bytes:
3232235521 -> 11000000.10101000.00000000.00000001 -> 192.168.0.1
192 168 0 1Syntax
long2ip(int $ip): string$ip— the 32-bit integer representing the IPv4 address. It must be anint; passing a string throws aTypeErrorin PHP 8+.- Return value — the address as a dotted-quad
string.
Basic Example
The integer 2130706433 decodes to the loopback address 127.0.0.1, which echo prints to the screen.
More Examples
A few common values make the mapping clearer:
<?php
echo long2ip(0), "\n"; // 0.0.0.0 (lowest possible)
echo long2ip(3232235521), "\n"; // 192.168.0.1 (private LAN)
echo long2ip(4294967295), "\n"; // 255.255.255.255 (highest possible)0 is the smallest 32-bit value and maps to 0.0.0.0; 4294967295 (which is 2^32 - 1) is the largest and maps to the broadcast-style 255.255.255.255.
Round-tripping with ip2long()
long2ip() is the inverse of ip2long(), which converts a dotted-quad string into its integer form. The two functions undo each other:
<?php
$ip = "8.8.8.8";
$long = ip2long($ip); // 134744072
echo long2ip($long); // 8.8.8.8 — back where we startedThis pairing is the typical workflow: store ip2long($address) as an integer column in your database, then call long2ip() when you need to display or log it.
The signed integer gotcha
On a 32-bit platform, ip2long() can return a negative number for addresses above 127.x.x.x, because the high bit is interpreted as a sign. You may also see negative values if an integer was stored in a signed database column. long2ip() accepts these signed values and still decodes them correctly:
<?php
echo long2ip(-1); // 255.255.255.255So you do not need to "fix" a negative integer before passing it to long2ip() — but be aware of where the negative came from, and prefer 64-bit platforms (the default today) where the unsigned value is preserved.
When would I use this?
- Storing addresses efficiently — an
INT UNSIGNED(orBIGINT) column is smaller and faster to index than aVARCHAR(15), and IP-range filtering (WHERE ip BETWEEN ? AND ?) becomes simple integer math. - Reading binary data — protocols and packet headers carry addresses as raw 32-bit integers;
long2ip()makes them readable. - Logging and analytics — convert stored integers back to strings only at display time.
For IPv6, long2ip() does not apply — IPv6 addresses are 128 bits. Use inet_ntop() / inet_pton() instead, which handle both IPv4 and IPv6. See the PHP networking functions chapter for an overview.
Conclusion
long2ip() turns a 32-bit integer back into a human-readable IPv4 address, the natural counterpart to ip2long(). Pair the two to store addresses compactly as integers while still displaying them as familiar dotted-quad strings, and remember that the function transparently handles the signed integers that 32-bit systems sometimes produce.