note 62316 deleted from function.eval by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 19:57:15 +0000 |
| Subject: | note 62316 deleted from function.eval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155299@lists.php.net to get a copy of this message | ||
Note Submitter: jnavratil at houston dot rr dot com
----
"A return statement will terminate the evaluation of the string immediately. As of PHP 4,
eval() returns NULL unless return is called in the evaluated code, in which case the value passed to
return is returned." isn't strictly true as the eval can return a value through the
side-effect of setting a variable.
<?php
function getValue()
{
return 123;
}
function testEval1()
{
eval('return getValue();');
}
function testEval2()
{
eval('$rslt = getValue();');
return $rslt;
}
function testEval3()
{
return eval('return getValue();');
}
function testEval4()
{
return eval('getValue();');
}
print '1:'.testEval1()."\n";
print '2:'.testEval2()."\n";
print '3:'.testEval3()."\n";
print '4:'.testEval4()."\n";
?>
results in...
1:
2:123
3:123
4:
In case 1, the eval returns the result to an uncaring caller (contra: case 3). In case 2, the eval
makes the result available through the side-effect of setting '$rslt'. In case 4,
'eval' returns null to an apparently caring caller (contra: case 1).