PHP long2ip function tutorial shows how to convert long integer IP addresses to dotted-quad strings in PHP. Learn long2ip with practical examples for network programming.
last modified April 4, 2025
The PHP long2ip function converts a long integer address into a dotted-quad string IP address. It’s essential for network programming.
long2ip transforms a 32-bit unsigned long integer into an IPv4 address string. The integer represents the packed binary format of an IP.
Syntax: long2ip(int $ip): string. Returns the IP address as a string in dotted format (e.g., “192.168.1.1”). Works with IPv4 only.
This example demonstrates the simplest usage of converting a long to IP.
basic_conversion.php
<?php
declare(strict_types=1);
$longIP = 2130706433; // 127.0.0.1 in long format $ipAddress = long2ip($longIP);
echo “Long: $longIP converts to IP: $ipAddress”;
This converts the integer 2130706433 to its IP representation 127.0.0.1. The function handles the binary to dotted-quad conversion automatically.
This shows how to convert IPs stored as integers in databases to readable format.
database_ip_conversion.php
<?php
declare(strict_types=1);
// Simulating IP stored as integer in database $storedIP = 3232235521; // 192.168.0.1
$readableIP = long2ip($storedIP); echo “Database value $storedIP represents IP: $readableIP”;
Many databases store IPs as integers for efficiency. This converts them back to human-readable format when retrieved. The example uses 192.168.0.1.
This demonstrates the relationship between ip2long and long2ip functions.
ip_conversion_cycle.php
<?php
declare(strict_types=1);
$originalIP = “10.0.0.138”; $longIP = ip2long($originalIP); $convertedIP = long2ip($longIP);
echo “Original: $originalIP\n”; echo “Long: $longIP\n”; echo “Converted back: $convertedIP”;
This shows the complete cycle of converting IP to long and back. The functions are inverses of each other when used correctly. Note potential signed integer issues on 32-bit systems.
This example converts network addresses stored as long integers.
network_address_conversion.php
<?php
declare(strict_types=1);
$networkLong = 2886729728; // 172.16.0.0 $broadcastLong = 2886733823; // 172.16.0.255
echo “Network: " . long2ip($networkLong) . “\n”; echo “Broadcast: " . long2ip($broadcastLong);
Network programming often works with address ranges as integers. This converts network and broadcast addresses to readable format. Useful for subnet calculations.
This demonstrates how long2ip handles invalid input values.
error_handling.php
<?php
declare(strict_types=1);
$invalidLong = -1; // Negative value $largeLong = 4294967296; // Exceeds 32-bit
try { echo “Negative: " . long2ip($invalidLong) . “\n”; echo “Large: " . long2ip($largeLong); } catch (Throwable $e) { echo “Error: " . $e->getMessage(); }
The function wraps around for values outside 0-4294967295 range due to PHP’s integer handling. Negative values produce unexpected results rather than errors.
Validation: Validate input is within 0-4294967295 range
32-bit Systems: Be aware of signed integer limitations
IPv6: Use other functions for IPv6 addresses
Type Safety: Ensure input is integer type
This tutorial covered the PHP long2ip function with practical examples for IP address conversion in network programming scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.