note 75628 deleted from function.eval by bjori

From: Date: Sun, 10 Jun 2007 15:16:39 +0000
Subject: note 75628 deleted from function.eval by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127349@lists.php.net to get a copy of this message
Note Submitter: nielsvan den berge at hotmail dot com Reason: bad code ---- To check if the evaluated code contains a (syntax-)error or returns the boolean false (in a "good"-script) I use this script: NOTE: It only checks if the last error has been changed, it's not a good error-handling method <?php test_eval("return false;"); test_eval("return true;"); test_eval("\$var = \"123\";"); test_eval("return NOT_DEFINED;"); test_eval("error code"); function test_eval($eval_code) { echo "<br />Evaluating: '".$eval_code."'<br />"; // Trigger a harmless notice-error and store it in the variable $last_error @trigger_error("", E_USER_NOTICE); $last_error = error_get_last(); $evaled = @eval($eval_code); // If the last error is still the same as our generated notice-error the eval code hasn't triggered an error if ($last_error == error_get_last()) { switch ($evaled) { case false : echo "eval returned false";break; case true : echo "eval returned true";break; case null : echo "eval didn't return anything (null)";break; } } else { $error = error_get_last(); echo "eval contains an error, it returned false<br />".$error['message']; } } ?> This code outputs the following: Evaluating: 'return false;' eval returned false Evaluating: 'return true;' eval returned true Evaluating: '$var = "123";' eval returned false Evaluating: 'return NOT_DEFINED;' eval contains an error, it returned false Use of undefined constant NOT_DEFINED - assumed 'NOT_DEFINED' Evaluating: 'error code' eval contains an error, it returned false parse error, unexpected T_STRING

« previous php.notes (#127349) next »