Thursday, September 4, 2025
HomeLanguagesPHP | inet_pton() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS