PHP | inet_pton() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Use inet_pton() function on IP
// address and get result
$addr = inet_pton("127.0.1.1");
  
echo $addr;
  
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>";
  
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.inet-pton.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.