PHP | inet_ntop() Function

The inet_ntop() function is an inbuilt function in PHP which converts a 32bit IPv4 or 128bit IPv6 address into a readable format.

Syntax:

string inet_ntop( string $ip_address )

Parameter: This function accepts one parameter as mentioned above and described below:



  • $ip_address: It is required parameter. It specifies a 32bit IPv4 or 128bit IPv6 address.

Return Value: This function returns a string formatted human readable address on success or FALSE on failure.

Note: This function is available on PHP 5.1.0 and newer version.

Below programs illustrate the inet_ntop() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Store the address into variable
$addr = chr(127) . chr(0) . chr(1) . chr(1);
  
// Use inet_ntop() function to convert
// internet address to a human readable
// representation
$exp = inet_ntop($addr);
  
// Display result
echo $exp;
  
?>

chevron_right


Output:

127.0.1.1

Program 2: This program uses a string of size 4 of ascii characters directly in the parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Use inet_ntop() function to convert
// internet address to a human readable
// representation
echo inet_ntop("[][]") . "<br>";
echo inet_ntop("4509") . "<br>";
echo inet_ntop("*^b@") . "<br>";
echo inet_ntop("hqp0") . "<br>";
echo inet_ntop("2c#!");
  
?>

chevron_right


Output:

91.93.91.93
52.53.48.57
42.94.98.64
104.113.112.48
50.99.35.33

Reference: https://www.php.net/manual/en/function.inet-ntop.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.