PHP long2ip() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, long2ip() converts a 32-bit integer back into an IPv4 dotted address string such as "127.0.0.1".

It is the reverse of ip2long() for IPv4. You pass the integer form and get a readable address. It does not handle IPv6. Pair it with ip2long() when you store addresses as numbers and need to show them again.

Syntax

Call long2ip() with the values you need. Start with this line.

PHP
$ip = long2ip(2130706433);
CallWhat it does
long2ip(2130706433)Returns "127.0.0.1".
long2ip(ip2long('8.8.8.8'))Round-trips an address through integer form and back.
long2ip(0)Returns "0.0.0.0".

Pass an integer in the IPv4 32-bit range. Values from ip2long() are the usual input. On modern PHP the return type is string|false; a normal in-range int yields a dotted string.

Library declarations
PHP
long2ip(int $ip): string|false

Parameters

ParameterDescription
ipRequired. A 32-bit IPv4 address as an integer, often produced by ip2long().

Return Value

Return ValueDescription
stringThe IPv4 address in dotted form, for example 192.168.0.1.
falseConversion failed (rare for a normal integer on current PHP).

PHP Examples

Example 1 — Turn Integers into IPv4 Strings

Convert a few well-known integers and print the dotted addresses.

PHP
<?php
echo long2ip(2130706433), PHP_EOL;
echo long2ip(134744072), PHP_EOL;
echo long2ip(0), PHP_EOL;

How It Works

  1. 2130706433 maps to 127. 0.0.1.
  2. 134744072 maps to 8. 8.8.8.
  3. 0 maps to 0. 0.0.0.
  4. Each result is a normal string you can echo or store.

Example 2 — Round-Trip with ip2long()

Convert an address to an integer and back to prove the pair works together.

PHP
<?php
$original = '10.20.30.40';
$num = ip2long($original);
$back = long2ip($num);

echo $original, PHP_EOL;
echo $num, PHP_EOL;
echo $back, PHP_EOL;
echo ($original === $back) ? 'match' : 'different', PHP_EOL;

How It Works

  1. ip2long() creates the integer form.
  2. long2ip() rebuilds the dotted string.
  3. The printed integer is the packed IPv4 value.
  4. Strict string compare shows the round-trip matched.

Example 3 — Build a Small Address List

Map an array of integers to dotted strings with array_map().

PHP
<?php
$nums = [2130706433, 134744072, 3232235521];

foreach (array_map('long2ip', $nums) as $ip) {
    echo $ip, PHP_EOL;
}

How It Works

  1. The array holds three IPv4 integers.
  2. array_map('long2ip', $nums) converts each one.
  3. The loop prints one dotted address per line.
  4. 192. 168.0.1 comes from the unsigned-style value 3232235521 on 64-bit PHP.

Frequently Asked Questions

It converts a 32-bit IPv4 integer into a dotted address string such as 127.0.0.1.
Yes for IPv4. ip2long() goes string to int, and long2ip() goes int back to string.
No. Use inet_ntop() with a packed binary address for IPv6.
Pass an integer, usually the return value of ip2long() after you checked it is not false.
On 32-bit PHP, ip2long() may return a negative int for high addresses. sprintf('%u', $num) shows the unsigned decimal for humans, while long2ip($num) still rebuilds the correct dotted IP from that same int.

Next: net_get_interfaces()

net_get_interfaces() lists the network interfaces on the local machine and returns details.

net_get_interfaces() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful