Jump to content

[SOLVED] Problems with returning true or false in eval()'d code


codebyren

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.