PHP inet_ntop() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, inet_ntop() converts a packed binary IP address into the human-readable presentation form for IPv4 or IPv6.

The input is the binary string you get from inet_pton() or from some network APIs. The output looks like "127.0.0.1" or "::1". Invalid binary input returns false.

Syntax

Call inet_ntop() with the values you need. Start with this line.

PHP
$text = inet_ntop($packed);
CallWhat it does
inet_ntop(inet_pton("127.0.0.1"))Round-trips an IPv4 address back to text.
inet_ntop(inet_pton("::1"))Round-trips the IPv6 loopback address.
inet_ntop("bad")Returns false for binary data that is not a valid address.

IPv4 packed addresses are 4 bytes long. IPv6 packed addresses are 16 bytes. Pair inet_ntop() with inet_pton() when you store or compare addresses in binary form.

Library declarations
PHP
inet_ntop(string $ip): string|false

Parameters

ParameterDescription
ipRequired. A packed binary IP address string (in_addr / in6_addr form).

Return Value

Return ValueDescription
string|falseThe presentation address string, or false if the binary value is invalid.

PHP Examples

Example 1 — Convert Packed IPv4 Back to Text

Pack 127.0.0.1 with inet_pton(), then unpack it with inet_ntop().

PHP
<?php
$packed = inet_pton("127.0.0.1");
echo inet_ntop($packed), PHP_EOL;

How It Works

  1. inet_pton() creates the 4-byte binary form.
  2. inet_ntop() turns that binary back into dotted text.
  3. The printed value matches the original address.
  4. This round-trip is the usual teaching demo for both functions.

Example 2 — Unpack IPv6 Loopback

Do the same round-trip for the IPv6 loopback address ::1.

PHP
<?php
$packed = inet_pton("::1");
echo inet_ntop($packed), PHP_EOL;

How It Works

  1. inet_pton("::1") returns 16 bytes of binary data.
  2. inet_ntop() formats it as ::1 again.
  3. PHP needs IPv6 support in the build, which is normal on modern hosts.
  4. Use var_dump() on $packed if you want to inspect the binary length.

Example 3 — Show Binary Length With bin2hex

Print the hex form of packed data beside the readable inet_ntop() result.

PHP
<?php
$packed = inet_pton("192.0.2.10");
echo bin2hex($packed), PHP_EOL;
echo inet_ntop($packed), PHP_EOL;

How It Works

  1. 192. 0.2.10 is a documentation IPv4 address.
  2. bin2hex() shows the four packed bytes as c000020a.
  3. inet_ntop() restores the dotted string.
  4. Hex dumps are handy when you store packed addresses in binary columns.

Frequently Asked Questions

It converts a packed binary IPv4 or IPv6 address into a readable string such as 127.0.0.1 or ::1.
inet_pton() converts presentation text into packed binary. inet_ntop() converts packed binary back to text.
The binary string is not a valid 4-byte IPv4 or 16-byte IPv6 address.
No. That is presentation text. Pack it with inet_pton() first, or use inet_ntop() only on packed data.
Pack both with inet_pton() and compare the binary strings, or unpack with inet_ntop() after normalizing through inet_pton().

Next: inet_pton()

inet_pton() converts a human-readable IPv4 or IPv6 address into its packed binary form.

inet_pton() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful