3v4l.org

run code in 500+ PHP versions simultaneously
<?php function convertToBytes(string $memoryLimit): int { $memoryLimit = strtolower($memoryLimit); $max = ltrim($memoryLimit, '+'); if (str_starts_with($max, '0x')) { $max = \intval($max, 16); } elseif (str_starts_with($max, '0')) { $max = \intval($max, 8); } else { $max = (int) $max; } switch (substr(rtrim($memoryLimit, 'b'), -1)) { case 't': $max *= 1024; // no break case 'g': $max *= 1024; // no break case 'm': $max *= 1024; // no break case 'k': $max *= 1024; } return $max; } var_dump(convertToBytes('1G')); var_dump(convertToBytes('1.5G'));
Output for 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
int(1073741824) int(1073741824)

preferences:
63.63 ms | 836 KiB | 4 Q