The inet_pton() function is an inbuilt function in PHP which converts a readable format IP address into a packed 32bit IPv4 or 128bit IPv6 address.
Syntax:
string inet_pton( string $ip_address )
Parameters: This function accepts single parameter as mentioned above and described below:
- $ip_address: It is required parameter. It specifies the readable IP address.
Return Value: This function returns a packed 32bit IPv4 or 128bit IPv6 address on success and FALSE on failure.
Note:
- This function is available for PHP 5.1.0 and newer version.
- Output of the following examples may differ in this page and actual output. So please see output screenshots.
Below programs illustrate the inet_pton() function in PHP:
Program 1:
<?php // Use inet_pton() function on IP // address and get result $addr = inet_pton( "127.0.1.1" ); echo $addr ; ?> |
Output:
Program 2:
<?php echo inet_pton( "" . gethostbyname ( "www.google.com" )). "<br>" ; echo inet_pton( "" . gethostbyname ( "www.youtube.com" )). "<br>" ; echo inet_pton( "" . gethostbyname ( "www.facebook.com" )). "<br>" ; echo inet_pton( "" . gethostbyname ( "www.geeksforgeeks.org" )). "<br>" ; echo inet_pton( "" . gethostbyname ( "php.net" )). "<br>" ; ?> |
Output:
Reference: https://www.php.net/manual/en/function.inet-pton.php