Open In App

PHP | long2ip() Function

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The long2ip() function is an inbuilt function in PHP which converts long integer into corresponding IPv4 address in string format. Syntax:
string long2ip( int $ip_in_long )
Parameters: This function accepts single parameter as mentioned above and described below:
  • $ip_in_long: It is required parameter. It specifies a long integer that represents an IP address.
Return Value: This function returns an IP address in string format. Note: This function is available on PHP 4.0.0 and newer versions. Below programs illustrate the long2ip() function in PHP: Program 1: php
<?php

// Use long2ip() function to converts
// long integer address into a string
// format
echo(long2ip(344294967296));
?>
Output:
41.148.72.0
Program 2: php
<?php

// Store the long integer address in an array
$hosts = array( 874081766, 3627733732, 3627734286,
                520968740, 2539994370, 2539979077);

$out = "List of IP addresses:";

foreach( $hosts as $host ) {
    
    $ip = long2ip($host);
    $hostname = gethostbyaddr($ip);
    
    $out .= "<br> https://" . $host . "/  https://" .
            $ip . "/  https://" . $hostname . "/";
}

echo $out;

?>
Output:
List of IP addresses:
https://874081766/ https://52.25.109.230/ 
                https://ec2-52-25-109-230.us-west-2.compute.amazonaws.com/
https://3627733732/ https://216.58.210.228/ https://mrs04s10-in-f228.1e100.net/
https://3627734286/ https://216.58.213.14/ https://lhr25s25-in-f14.1e100.net/
https://520968740/ https://31.13.90.36/ https://edge-star-mini-shv-01-lhr3.facebook.com/
https://2539994370/ https://151.101.61.2/ https://151.101.61.2/
https://2539979077/ https://151.101.1.69/ https://151.101.1.69/
Reference: https://www.php.net/manual/en/function.long2ip.php

Next Article

Similar Reads