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


Posted on 16th Feb 2014 07:03 pm by admin

Hi guys,

Would appreciate some help with a problem when running eval() on a function that should return true or false.

From php.net:
Quoteeval() 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:

Code: <?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...

No comments posted yet

Your Answer:

Login to answer
117 Like 9 Dislike
Previous forums Next forums
Other forums

Help With Showing Users On the Index Page
Ive got this code which works just how i want it to.

Code: <?
$timenow=time();

batch file not building to webapps folder
Hello. I just started running Vista :P and can't seem to get my projects deployed. I found out how

Shuffle Array
Hi,

I am writing a script for a game that needs players to randomly be assigned a target (ano

script is pulling more then they have....
I have this script for my game where they can change there race. This will cost them 2,500 points. H

Snapshot from video
anyone knw how to make a snapshot maker from avi n mkv format.
for example if i give a direct lin

Got A Free Server :(
So I was given a free server

http://shopping.yahoo.com/p:Compaq%20ProLiant%201500%20Server:1

I need some help with the IF command...
I have a large number of images in one section of my website with the .jpg format. I'm adding a bun

Slashes
Have a small problem and I'm not able to understand why I'm getting the results I'm getting... and i

I need desperate help with this php code integrated with flash please
Can someone please help me with this php code which I have been stuck on for weeks . I am trying to

writing a screen scraper
Hello,

I'm writing a screen scraper application and want to be able to get absolute addresses

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash