Parse INI Quantity Values using ini_parse_quantity in PHP 8.2

Parse INI Quantity Values using ini_parse_quantity in PHP 8.2

Some of the PHP INI directives also accepts shorthand byte values, such as 1K for 1 kilobyte, or 10M for 10 megabytes, etc. 1K equals 1024 bytes. Available options are K or k (kilobytes), M or m (megabytes) and G or g (gigabytes). Shorthand notations can be used in php.ini file or with ini_set function.

Since PHP 8.2, we can use ini_parse_quantity function that allows to parse INI quantity values. The function returns interpreted size in bytes.

<?php

echo ini_parse_quantity('1024').PHP_EOL; // 1024
echo ini_parse_quantity('1K').PHP_EOL; // 1024
echo ini_parse_quantity('128M').PHP_EOL; // 134217728
echo ini_parse_quantity('2G').PHP_EOL; // 2147483648

Leave a Comment

Cancel reply

Your email address will not be published.