3v4l.org

run code in 200+ php & hhvm versions
<?php $input = '20-5*2/30+9='; $inputLength = mb_strlen($input); $number = 0; $result = 0; $op = ''; for ($i = 0; $i < $inputLength; $i++) { $char = mb_substr($input, $i, 1); if ($char == '*' || $char == '+' || $char == '-' || $char == '=' || $char == '/') { if ($op == '') { $result = $number; } elseif ($op == '+') { $result += $number; } elseif ($op == '-') { $result -= $number; } elseif ($op == '*') { $result *= $number; } elseif ($op == '/') { $result /= $number; } echo "op[$op] result[$result] number[$number] char[$char] \n"; $op = $char; $number = 0; if ($char == '=') { echo "Ответ: $input$result"; } } elseif (is_numeric($char) || $char == '.') { $number = $number * 10 + intval($char); } else { echo "Неверный символ:'$char\n"; exit(); } }
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.28 - 7.2.9
op[] result[20] number[20] char[-] op[-] result[15] number[5] char[*] op[*] result[30] number[2] char[/] op[/] result[1] number[30] char[+] op[+] result[10] number[9] char[=] Ответ: 20-5*2/30+9=10