PHP ip2long() Function
The PHP ip2long() function converts IPv4 address (dotted IP address) into a long integer.
Syntax
ip2long(ip)
Parameters
ip |
Required. Specify the standard IP address. |
Return Value
Returns the long integer or false if the IP address is invalid.
Example:
The example below shows the usage of ip2long() function.
<?php $host="alphacodingskills.com"; $ip = gethostbyname($host); echo "The following URLs are equivalent: \n"; echo "1. https://".$host."\n". "2. https://".$ip."\n". "3. https://".sprintf("%u", ip2long($ip))."/"; ?>
The output of the above code will be:
The following URLs are equivalent: 1. https://alphacodingskills.com 2. https://35.192.78.222 3. https://599805662/
Example:
Consider one more example where this function is used with a number of hostnames.
<?php $hosts=["alphacodingskills.com", "google.com", "youtube.com", "facebook.com"]; $out = "Equivalent Contents:"; foreach($hosts as $host) { $ip = gethostbyname($host); $out .= "\nhttps://". $host ."/ https://". $ip . "/ https://". sprintf("%u", ip2long($ip)) ."/"; } echo $out; ?>
The output of the above code will be:
Equivalent Contents: https://alphacodingskills.com/ https://35.192.78.222/ https://599805662/ https://google.com/ https://173.194.193.139/ https://2915221899/ https://youtube.com/ https://142.250.128.91/ https://2398781531/ https://facebook.com/ https://157.240.2.35/ https://2649752099/
❮ PHP Network Reference