Hi Lawrence,
I think there really ought to be a check() function that parses but does not
evaluate PHP code. But AFAIK there isn't.
Since the eval command is executing in the same environment as the rest of
the PHP code, when it returns an error and dies it affects the whole
environment. So, the workaround solution is to execute a sub-process to
evaluate the code and return the result to the main process. This can be
done using exec and command-line PHP:
<?PHP
$string1 = "php -r 'pint ('foo');'";
$string2 = "php -r 'print ('foo');'";
$result1 = exec($string1);
$result2 = exec($string2);
print $string1." returned: ".$result1."<br />\n";
print $string2." returned: ".$result2."<br />\n";
?>
From here you can probably create your checkPHP() function by grepping the
output for words like "Fatal error." Better would be if you know the
expected output to grep for that. Or maybe you can check the command-line
PHP exit status. Use the return_var for this.
Either way by spawning a sub-process to evaluate your code you are saved
from this affecting the main environment and therefore well on your way to a
solution.
Good luck.
Cheers,
Robert
On 3/8/04 3:31 PM, in article
da**************************@posting.google.com, "lawrence"
<lk******@geocities.com> wrote:
I have a string which I want to send to eval(). How can I test it
ahead of time to make sure it is valid code? I don't want to send it
to eval and get parse errors. I want to do something like this:
$valid = checkPHP($string);
if ($valid) {
eval($string);
} else {
$resultsObject->addToErrorResults("We wanted to send our
template to eval(), but the PHP it contained was invalid.");
}
Is there anything like checkPHP()?