PHP ip2long() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, ip2long() turns an IPv4 address string such as "192.168.0.1" into a 32-bit integer that is handy for storage, range checks, or math on addresses.

It only accepts IPv4 dotted addresses. IPv6 strings are not supported. On failure it returns false. On 32-bit PHP the integer can look negative for high addresses; use sprintf('%u', ...) when you need the unsigned decimal form for display.

Syntax

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

PHP
$num = ip2long('192.168.0.1');
CallWhat it does
ip2long('127.0.0.1')Returns 2130706433, the integer form of localhost.
ip2long('192.168.0.1')Returns a signed or unsigned 32-bit value depending on the PHP build.
ip2long('not-an-ip')Returns false because the text is not a valid IPv4 address.

Pass a dotted IPv4 string only. Check the result with !== false before you use it as a number. For an unsigned printable value, sprintf('%u', $num) works on both 32-bit and 64-bit PHP when $num came from ip2long().

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

Parameters

ParameterDescription
ipRequired. An IPv4 address in dotted form, for example 8.8.8.8 or 127.0.0.1.

Return Value

Return ValueDescription
intThe 32-bit IPv4 address as a PHP integer. On 32-bit builds this may be negative for addresses with the high bit set.
falseThe string was not a valid IPv4 address.

PHP Examples

Example 1 — Convert a Few IPv4 Addresses

Call ip2long() on common addresses and print the integers with var_export().

PHP
<?php
var_export(ip2long('127.0.0.1'));
echo PHP_EOL;
var_export(ip2long('8.8.8.8'));
echo PHP_EOL;
var_export(ip2long('0.0.0.0'));
echo PHP_EOL;

How It Works

  1. ip2long('127. 0.0.1') becomes 2130706433.
  2. ip2long('8. 8.8.8') becomes 134744072.
  3. ip2long('0. 0.0.0') becomes 0.
  4. var_export() prints the raw integer so you can see the exact type.

Example 2 — Invalid Input and Unsigned Display

Reject a bad string, then show how sprintf('%u') prints a high IPv4 value as unsigned decimal.

PHP
<?php
var_export(ip2long('999.1.1.1'));
echo PHP_EOL;

$num = ip2long('192.168.0.1');
if ($num !== false) {
    echo 'signed-ish: ', $num, PHP_EOL;
    echo 'unsigned: ', sprintf('%u', $num), PHP_EOL;
}

How It Works

  1. An out-of-range octet makes ip2long() return false.
  2. We store a valid conversion and check !== false.
  3. On 64-bit PHP the number is already positive 3232235521.
  4. sprintf('%u', $num) is the portable way to print the unsigned form on 32-bit builds too.

Example 3 — Compare Two Addresses as Numbers

Convert two IPs and see which integer is larger. This is useful for simple range logic.

PHP
<?php
$a = ip2long('10.0.0.5');
$b = ip2long('10.0.0.20');

echo 'a: ', $a, PHP_EOL;
echo 'b: ', $b, PHP_EOL;
echo ($a < $b) ? '10.0.0.5 comes first' : 'order differs', PHP_EOL;

How It Works

  1. Both dotted addresses convert to integers.
  2. Smaller host numbers in the same /24 give smaller integers.
  3. A normal < comparison works once both values are ints.
  4. Always confirm both conversions succeeded before comparing in real code.

Frequently Asked Questions

It converts an IPv4 dotted address string into a 32-bit integer, or returns false if the string is not valid IPv4.
No. It is IPv4 only. For IPv6 packed forms use inet_pton() and inet_ntop() instead.
On 32-bit PHP the value is a signed int, so addresses with the high bit set appear negative. sprintf('%u', $num) prints the unsigned decimal for teaching and display.
Use $num = ip2long($ip); if ($num === false) { ... }. Do not use empty() alone, because 0.0.0.0 correctly becomes 0.
When you want a stable unsigned decimal string for logs or lessons. Keep the original int for math, and remember the format is for display, not a different network type.

Next: long2ip()

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

long2ip() 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