</p> <ul> <li>Only numbers and +,-,* and / operators are allowed in the expression.</li> <li>The evaluation follows the standard operator precedence.</li> <li>The calculator does not support parentheses.</li> <li>The calculator handles invalid input "gracefully". It does not output PHP error messages.</li> </ul> <p> <input type="text" name="expression"> <input type="submit" value="Calculate" name="submit"> </p> <?php if (isset($_POST['submit']) && isset($_POST['expression'])) { check_expression($_POST['expression']); } function check_expression($expr) { /*GLOBAL VARIABLES*/ $invalid = false; /*1. CHECK FOR INVALID CHARACTERS ERROR */ /* allowed characters: 1.+ 2.- 3.* 4./ 5.0-9 */ if (preg_match("/[^\\+\\-\\*\\/\\.0-9]/", trim($expr), $matches, PREG_OFFSET_CAPTURE)) {
function scan_extractive_expressions($variable) { if (!is_array($variable)) { return; } if ($variable == array()) { return; } if ($variable[0] == 'ARRAY_AT' || $variable[0] == 'CHAR_OF_STRING') { check_expression($variable[1]); } if (($variable[0] == 'ARRAY_AT' || $variable[0] == 'DEREFERENCE') && count($variable[2]) != 0) { scan_extractive_expressions($variable[2]); } }