Content · Programming · PHP

PHP inet_ntop() Function: Usage and Examples

Understand PHP inet_ntop() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use inet_ntop() when you need to converts a packed binary IPv4 or IPv6 address into readable text in a PHP application. The examples below show the actual operation and its limitations.

What Is inet_ntop() in PHP?

Converts a packed binary IPv4 or IPv6 address into readable text. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

inet_ntop(string $ip): string|false

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

echo inet_ntop(inet_pton("127.0.0.1"));

Output:

127.0.0.1

Example 2: Different Input or Behavior

<?php

echo inet_ntop(inet_pton("2001:db8::1"));

Output:

2001:db8::1

Practical Example: Applying the Function

<?php

$packedAddress = inet_pton("192.0.2.15"); echo $packedAddress === false ? "Invalid" : inet_ntop($packedAddress);

Output:

192.0.2.15

Important Behavior and Edge Cases

The input must be a valid packed binary IP; check for false before converting.

Comparison With Related Functions

ApproachDifference
inet_ntop()Converts a packed binary IPv4 or IPv6 address into readable text.
Related operationinet_pton() converts a readable address to packed binary.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationThe input must be a valid packed binary IP; check for false before converting.
Confusing this function with a related operationinet_pton() converts a readable address to packed binary.

Key Takeaways

  • Converts a packed binary IPv4 or IPv6 address into readable text.
  • The input must be a valid packed binary IP; check for false before converting.
  • inet_pton() converts a readable address to packed binary.

Use inet_ntop() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.