PHP inet_pton() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

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

Use it when you need a compact binary value for storage, comparison or socket APIs. Invalid address text returns false (and may raise a warning depending on error settings).

Syntax

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

PHP
$packed = inet_pton("127.0.0.1");
CallWhat it does
inet_pton("127.0.0.1")Returns 4 bytes of packed IPv4 data.
inet_pton("::1")Returns 16 bytes of packed IPv6 data.
inet_pton("not-an-ip")Returns false for invalid input.

Presentation strings must be plain addresses. CIDR suffixes such as 192.0.2.0/24 are not accepted. Convert back to text with inet_ntop().

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

Parameters

ParameterDescription
ipRequired. A readable IPv4 or IPv6 address string.

Return Value

Return ValueDescription
string|falseThe packed binary address, or false if the string is not a valid IP address.

PHP Examples

Example 1 — Pack an IPv4 Address

Convert 127.0.0.1 to packed form and show it with bin2hex().

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

How It Works

  1. inet_pton() accepts the dotted IPv4 string.
  2. bin2hex() prints the four bytes as 7f000001.
  3. strlen() confirms the packed value is 4 bytes.
  4. Keep the binary string itself for comparisons or storage.

Example 2 — Invalid Address Returns False

Pass a bad string and show the false result with var_dump().

PHP
<?php
$result = @inet_pton("192.0.2");
var_dump($result);

How It Works

  1. 192. 0.2 is not a complete IPv4 address.
  2. inet_pton() returns false.
  3. The @ sign quiets the warning for a clean demo printout.
  4. In real code, handle false explicitly instead of silencing errors forever.

Example 3 — Pack IPv6 and Round-Trip

Pack ::1, print its length, then convert back with inet_ntop().

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

How It Works

  1. IPv6 packed addresses are 16 bytes long.
  2. inet_ntop() restores the presentation form ::1.
  3. The same pattern works for full IPv6 text addresses.
  4. Use this pair whenever you need binary storage with readable display.

Frequently Asked Questions

It converts a readable IPv4 or IPv6 address into packed binary. Invalid input returns false.
Use bin2hex($packed) for a hex dump, or inet_ntop($packed) for the normal address text.
No. Values like 10.0.0.0/8 are invalid for inet_pton(). Strip the prefix length first.
ip2long() only handles IPv4 and returns an integer. inet_pton() supports IPv4 and IPv6 and returns binary strings.
The string was not a valid IPv4 or IPv6 presentation address. Check your input before packing.

Next: ip2long()

ip2long() turns an IPv4 address string such as "192.168.0.1" into a 32-bit integer that is.

ip2long() 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