codebyren Posted October 30, 2009 Share Posted October 30, 2009 Hi guys, Would appreciate some help with a problem when running eval() on a function that should return true or false. From php.net: eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. Not sure if I'm missing something here but the following code illustrates that the function returns true/false as expected when run directly. However, the same function run using eval() never returns true: <?php // Function to test what eval() returns... function test() { $test = rand(1, 3); return ($test === 1) ? TRUE : FALSE; } $eval = "test();"; $test = eval($eval); # expect this should set $test to TRUE for 1 in 3 attempts echo ($test) ? 'eval returned true' : 'eval returned FALSE or NULL <br />'; // Now retest the function by calling it directly, not through eval... $test2 = test(); echo ($test2) ? 'test returned TRUE' : 'test returned FALSE or NULL <br />'; # this behaves as expected... ?> Hopefully it's something ridiculously obvious to someone out there... Cheers for any feedback. Link to comment https://forums.phpfreaks.com/topic/179595-solved-problems-with-returning-true-or-false-in-evald-code/ Share on other sites More sharing options...
sasa Posted October 30, 2009 Share Posted October 30, 2009 change line $eval = "test();"; to $eval = "return test();"; Link to comment https://forums.phpfreaks.com/topic/179595-solved-problems-with-returning-true-or-false-in-evald-code/#findComment-947648 Share on other sites More sharing options...
codebyren Posted October 30, 2009 Author Share Posted October 30, 2009 You genius. Thanks. Link to comment https://forums.phpfreaks.com/topic/179595-solved-problems-with-returning-true-or-false-in-evald-code/#findComment-947656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.