Example 1 — Pack an IPv4 Address
Convert 127.0.0.1 to packed form and show it with bin2hex().
<?php
$packed = inet_pton("127.0.0.1");
echo bin2hex($packed), PHP_EOL;
echo strlen($packed), PHP_EOL; How It Works
- inet_pton() accepts the dotted IPv4 string.
- bin2hex() prints the four bytes as 7f000001.
- strlen() confirms the packed value is 4 bytes.
- Keep the binary string itself for comparisons or storage.

