PHP inet_pton() Function: Everything You Need to Know
As a PHP developer, you may need to convert an IP address from a human-readable format to a binary string. The inet_pton() function is a built-in function in
The PHP inet_pton() function converts a human-readable IP address — such as 127.0.0.1 or 2001:db8::1 — into its packed binary representation. The name stands for "presentation to network": it takes the presentation form (the dotted/colon text you type) and returns the network form (the raw bytes that actually travel on the wire).
This page covers the syntax, both IPv4 and IPv6, how to read the result safely, how to reverse it, and the practical reasons you would store IPs in binary at all.
What is the inet_pton() Function?
inet_pton() parses a valid IPv4 or IPv6 string and returns a packed in-addr binary string — 4 bytes for IPv4, 16 bytes for IPv6. If the input is not a valid IP address, it returns false instead of throwing.
The "binary string" it returns is not human-readable: it is the raw bytes, so printing it directly to a terminal or browser shows garbage. To inspect it, convert it with bin2hex(). To turn it back into a normal IP string, use its mirror function, inet_ntop().
Syntax
inet_pton(string $ip): string|falseThe function takes one parameter:
$ip— the IP address to convert, as a string (IPv4 or IPv6).
Return value: the packed binary string on success, or false if $ip is not a valid IP address.
Basic example: IPv4
The raw 4 bytes of 127.0.0.1 are 7f 00 00 01 — exactly the decimal octets 127.0.0.1 written in hexadecimal. Because the binary string itself is not printable, we run it through bin2hex() so the bytes show up as readable hex.
IPv4 and IPv6
Unlike the older ip2long(), which only handles IPv4, inet_pton() works for both address families. IPv4 produces 4 bytes; IPv6 produces 16:
<?php
echo bin2hex(inet_pton("192.168.1.1")), "\n"; // c0a80101 (4 bytes)
echo bin2hex(inet_pton("2001:db8::1")), "\n"; // 20010db8000000000000000000000001 (16 bytes)
echo strlen(inet_pton("192.168.1.1")), "\n"; // 4
echo strlen(inet_pton("2001:db8::1")), "\n"; // 16Checking strlen() on the result is the simplest way to tell which family an address belongs to: a length of 4 means IPv4, 16 means IPv6.
Handling invalid input
When the string is not a valid IP, inet_pton() returns false. Always check the result before using it — otherwise you may end up storing or comparing false (an empty string) by accident:
<?php
$input = "not-an-ip";
$packed = inet_pton($input);
if ($packed === false) {
echo "Invalid IP address";
} else {
echo bin2hex($packed);
}
// Outputs: Invalid IP addressUse strict comparison (===) here. A valid 0.0.0.0 packs to the byte 00000000, whose first byte is a null byte; a loose == false check could misfire on edge cases, so === false is the safe test.
Reversing the conversion
inet_pton() and inet_ntop() are a matched pair: one packs, the other unpacks. Round-tripping returns the original address:
<?php
$packed = inet_pton("2001:db8::1");
echo inet_ntop($packed); // Outputs 2001:db8::1When would I use this?
Storing IPs as packed binary is mainly about compact, sortable, family-agnostic storage:
- Database storage. A binary column (
VARBINARY(16)) holds any IPv4 or IPv6 address in a fixed, compact form and sorts correctly. Useinet_pton()beforeINSERTandinet_ntop()afterSELECT. - Range comparisons. Because the bytes preserve numeric order, you can compare packed strings to check whether an address falls inside a subnet.
- Exact-match lookups. Comparing fixed-length binary keys is faster and avoids the ambiguity of text forms (e.g.
2001:db8::1vs2001:0db8:0000:...:0001are the same address but different strings — packing normalizes them).
For IPv4-only numeric math, ip2long() and long2ip() are an alternative, but they cannot represent IPv6.
Conclusion
inet_pton() converts an IPv4 or IPv6 address from its human-readable text form into a packed binary string — 4 bytes for IPv4, 16 for IPv6 — and returns false on invalid input. Pair it with inet_ntop() to reverse the conversion, and use bin2hex() when you need to inspect the raw bytes. It is the standard way to store and compare IP addresses compactly in a database while supporting both address families.