W3docs

PHP ip2long() Function: Everything You Need to Know

As a PHP developer, you may need to convert an IP address from a human-readable format to a long integer. The ip2long() function is a built-in function in PHP

As a PHP developer, you may need to convert an IP address from a human-readable format to a long integer. The ip2long() function is a built-in PHP function that performs this conversion. This article covers its syntax, usage, and important caveats.

What is the ip2long() Function?

The ip2long() function converts a standard IPv4 address string (dotted-quad notation, like 192.168.0.1) into its corresponding 32-bit integer.

An IPv4 address is really just four bytes. ip2long() packs those four bytes into a single number: the first octet becomes the most significant byte, the last octet the least significant. So 127.0.0.1 becomes 127 × 256³ + 0 × 256² + 0 × 256 + 1 = 2130706433.

Why convert an IP to an integer?

Storing IP addresses as integers instead of strings has real advantages:

  • Smaller storage: an INT UNSIGNED column is 4 bytes versus up to 15 characters for the string form.
  • Faster comparisons and range queries: checking whether an IP falls inside a CIDR range becomes simple integer math (WHERE ip BETWEEN :start AND :end).
  • Indexing: integer indexes are more compact and faster than string indexes.

If you only need to display the address, keep it as a string. The integer form pays off when you store, sort, or range-match many addresses.

How to Use the ip2long() Function

Using the ip2long() function is straightforward. Here is the syntax:

The PHP syntax of ip2long() Function

ip2long($ip_address);

The function takes one parameter:

  • $ip_address: The IP address to be converted to a long integer.

Here is an example of how to use the ip2long() function with proper validation and unsigned formatting:

How to Use the ip2long() Function?

php— editable, runs on the server

In this example, we convert the IP address "127.0.0.1" to a long integer. We check if the result is false (which indicates invalid input) and use sprintf('%u', ...) to output the value as an unsigned integer.

Converting back: ip2long() and long2ip()

ip2long() has a mirror function, long2ip(), that turns the integer back into a dotted string. Together they round-trip cleanly:

<?php

$ip = "192.168.1.1";

$int = ip2long($ip);              // signed 32-bit value
$stored = sprintf('%u', $int);    // unsigned string for storage: 3232235777

echo $stored . "\n";              // 3232235777
echo long2ip($int) . "\n";        // 192.168.1.1

Note that long2ip() accepts the signed value returned by ip2long() directly, so you do not have to convert the unsigned form back before calling it.

Important Notes

  • Unsigned Output: ip2long() returns a signed 32-bit integer. For IP addresses in the upper range (e.g., 192.168.1.1), it may return a negative number. Always use sprintf('%u', ip2long($ip)) when storing or displaying the result.
  • IPv6 Support: ip2long() only supports IPv4 addresses. For IPv6, use the inet_pton() function instead.

Conclusion

The ip2long() function provides a straightforward way to convert IPv4 addresses to integers for storage or comparison. Remember to validate the return value and use sprintf('%u', ...) for consistent unsigned output. We hope this guide helps you implement IP address conversion effectively in your PHP applications.

Practice

Practice
What does the PHP function 'ip2long' do?
What does the PHP function 'ip2long' do?
Was this page helpful?