3v4l.org

run code in 300+ PHP versions simultaneously
<?php $expression = "-11+3*1*4/-6-12"; if (!preg_match('~^-?\d*\.?\d+([*/+-]-?\d*\.?\d+)*$~', $expression)) { echo "invalid expression"; } else { $components = preg_split('~(?<=\d)([*/+-])~', $expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); var_export($components); // ['-11','+','3','*','1','*','4','/','-6','-','12'] while (($index = array_search('*',$components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] * $components[$index + 1]); var_export($components); // ['-11','+','3','*','4','/','-6','-','12'] // ['-11','+','12','/','-6','-','12'] } while (($index = array_search('/', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] / $components[$index + 1]); var_export($components); // [-'11','+','-2','-','12'] } while (($index = array_search('+', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] + $components[$index + 1]); var_export($components); // ['-13','-','12'] } while (($index = array_search('-', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] - $components[$index + 1]); var_export($components); // [-25] } echo current($components); // -25 }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array ( 0 => '-11', 1 => '+', 2 => '3', 3 => '*', 4 => '1', 5 => '*', 6 => '4', 7 => '/', 8 => '-6', 9 => '-', 10 => '12', )array ( 0 => '-11', 1 => '+', 2 => 3, 3 => '*', 4 => '4', 5 => '/', 6 => '-6', 7 => '-', 8 => '12', )array ( 0 => '-11', 1 => '+', 2 => 12, 3 => '/', 4 => '-6', 5 => '-', 6 => '12', )array ( 0 => '-11', 1 => '+', 2 => -2, 3 => '-', 4 => '12', )array ( 0 => -13, 1 => '-', 2 => '12', )array ( 0 => -25, )-25
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array ( 0 => '-11', 1 => '+', 2 => '3', 3 => '*', 4 => '1', 5 => '*', 6 => '4', 7 => '/', 8 => '-6', 9 => '-', 10 => '12', )array ( 0 => '-11', 1 => '+', 2 => 3, 3 => '*', 4 => '4', 5 => '/', 6 => '-6', 7 => '-', 8 => '12', )array ( 0 => '-11', 1 => '+', 2 => 12, 3 => '/', 4 => '-6', 5 => '-', 6 => '12', )array ( 0 => '-11', 1 => '+', 2 => -2, 3 => '-', 4 => '12', )array ( 0 => -13, 1 => '-', 2 => '12', )array ( 0 => -25, )-25

preferences:
198.08 ms | 403 KiB | 156 Q