Skip to content

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 PHP function that performs this conversion. In this article, we will take an in-depth look at the long2ip() function and its usage.

What is the long2ip() Function?

The long2ip() function is a PHP built-in function that converts a long integer representing an IP address into a human-readable dotted-quad string.

How to Use the long2ip() Function

Using the long2ip() function is straightforward. Here is the syntax of the function:

The PHP syntax of long2ip() Function

php
long2ip($long_integer);

The function takes one parameter:

  • $long_integer: The long integer representing the IP address to be converted. This parameter accepts an int and the function returns a string.

Here is an example of how to use the long2ip() function to convert a long integer to an IP address:

How to Use the long2ip() Function?

php
<?php

$long_integer = 2130706433;
$ip_address = long2ip($long_integer);
echo $ip_address; // Outputs 127.0.0.1

In this example, we use the long2ip() function to convert the long integer 2130706433 to an IP address. We then use the echo statement to output the result (127.0.0.1) to the screen.

Note: The inverse function ip2long() converts an IP address back to a long integer. Depending on your system architecture, ip2long() may return a signed or unsigned 32-bit integer; long2ip() handles both correctly.

Conclusion

The long2ip() function is a useful tool for converting a long integer representing an IP address to a human-readable format in your PHP web application. By understanding the syntax and usage of the function, you can easily convert long integers to IP addresses. We hope this article has been informative and useful in understanding the long2ip() function in PHP.

Practice

What does the PHP long2ip() function do?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.