3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php if ( ! isset($argv[1]) || ! isset($argv[2])){ exit("An argument was expected.");}$base = substr($argv[1], 0, -1);$unit = substr($argv[1], strlen($argv[1])-1, 1);if ( ! is_numeric($argv[1][strlen($argv[1])-2])){ $base = substr($argv[1], 0, -2); $unit = substr($argv[1], strlen($argv[1])-2, 2);}if ( strlen($argv[1]) >= 3 && ! is_numeric($argv[1][strlen($argv[1])-3])){ $base = substr($argv[1], 0, -3); $unit = substr($argv[1], strlen($argv[1])-3, 3);}$targetUnit = $argv[2];if ( ! is_numeric($base) || is_float($base) === true){ exit("The argument is not a valid number.");}switch ($unit){ case 'GB': case 'GiB': $bits = $base * 1024 * 1024 * 1024 * 8; break; case 'Gb': case 'Gib': $bits = $base * 1024 * 1024 * 1024; break; case 'gB': $bits = $base * 1000 * 1000 * 1024 * 8; break; case 'gb': $bits = $base * 1000 * 1000 * 1024; break; case 'MB': case 'MiB': $bits = $base * 1024 * 1024 * 8; break; case 'Mb': case 'Mib': $bits = $base * 1024 * 1024; break; case 'mB': $bits = $base * 1000 * 1024 * 8; break; case 'mb': $bits = $base * 1000 * 1024; break; case 'KB': case 'KiB': $bits = $base * 1024 * 8; break; case 'Kb': case 'Kib': $bits = $base * 1024; break; case 'kB': $bits = $base * 1000 * 8; break; case 'kb': $bits = $base * 1000; break; case 'B': $bits = $base * 8; break; case 'b': $bits = $base; break; default: exit("Unit {$unit} is not supported.");}switch ($targetUnit){ case 'GB': case 'GiB': $resultBase = $bits / 1024 / 1024 / 1024 / 8; break; case 'Gb': case 'Gib': $resultBase = $bits / 1024 / 1024 / 1024; break; case 'gB': $resultBase = $bits / 1000 / 1000 / 1024 / 8; break; case 'gb': $resultBase = $bits / 1000 / 1000 / 1024; break; case 'MB': case 'MiB': $resultBase = $bits / 1024 / 1024 / 8; break; case 'Mb': case 'Mib': $resultBase = $bits / 1024 / 1024; break; case 'mB': $resultBase = $bits / 1000 / 1024 / 8; break; case 'mb': $resultBase = $bits / 1000 / 1024; break; case 'KB': case 'KiB': $resultBase = $bits / 1024 / 8; break; case 'Kb': case 'Kib': $resultBase = $bits / 1024; break; case 'kB': $resultBase = $bits / 1000 / 8; break; case 'kb': $resultBase = $bits / 1000; break; case 'B': $resultBase = $bits / 8; break; case 'b': $resultBase = $bits; break; default: exit("Unit {$targetUnit} is not supported.");}//$resultBase = number_format($resultBase, 0, '', '');echo "{$resultBase}{$targetUnit}";
Output for 5.4.0 - 7.1.0
An argument was expected.