3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $test = '2+3*pi'; // Remove whitespaces $test = preg_replace('/\s+/', '', $test); $number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number $functions = '(?:sinh?|cosh?|tanh?|abs|acosh?|asinh?|atanh?|exp|log10|deg2rad|rad2deg|sqrt|ceil|floor|round)'; // Allowed PHP functions $operators = '[+\/*\^%-]'; // Allowed math operators $regexp = '/^(('.$number.'|'.$functions.'\s*\((?1)+\)|\((?1)+\))(?:'.$operators.'(?2))?)+$/'; // Final regexp, heavily using recursive patterns if (preg_match($regexp, $q)) { $test = preg_replace('!pi|π!', 'pi()', $test); // Replace pi with pi function eval('$result = '.$test.';'); } else { $result = false; }
Output for 5.0.4 - 5.6.28, hhvm-3.10.0, 7.0.0 - 7.1.0
Notice: Undefined variable: q in /in/hmWEd on line 12
Output for hhvm-3.12.0 - 3.13.2
Notice: Undefined variable: q in /in/hmWEd on line 12 Warning: preg_match() expects parameter 2 to be string, null given in /in/hmWEd on line 12
Output for 4.3.0 - 5.0.3
Notice: Undefined variable: q in /in/hmWEd on line 12