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.

Did you know?Explore Trending and Topic pages for more stories like this.
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

form submit at godaddy?
Trying to test form submission at GoDaddy and it does not seem to work. Here's the page...

h

Check something, wait, check again, do something!
Hi guys.

I wonder if someone can help me with this.

Basically, what I want to do is (

Help with PHP Email Script
Hi guys,

I'm a newbie to php and need help with the script below:

At the moment when I

Formatting echo from database
So I have a database that stores First and last names, then echos them back to a website, as of now

Reading Most Recent CSV File in Directory
I thought I had wrapped this project up, but found out that the program I use to FTP a csv file to m

error checking breaking my code
Hi there, OK first of all, big apologies for what I assume is really fundamental errors in the struc

show the direct link into href
I want to display link on the page. i am fetching the data(URL) from database,actually before that i

MVC - Code review
I'm in the process of trying to wrap my head around MVC, and as part of that, I'm attempting to impl

function to return url
Hi,

Can you guys help me giving a function that takes all the contents in a particular field

a multi dimensional array with for each
hi, I have been asked to write an array, I have Zone 2,3,4,5,6,7,8 each zone has 3 sections of weigh

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