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

Taking an HTML form and a PHP program and making it into one working file.
Hello,

So I would like to display everything INSIDE a specific directory. In other words, I

start new row of pictures
on my site www.budstreasureauctions.com there is a featured auctions box that users will pay to get

update sql when refresh - php
hi
I have made a table (attachement)

the users can update the sql database using + or x bu

is_dir() problem
Hello,

I'm buidling a php scripts that dynamically get's subfolders from a specific folder.

problem with php mysql query
Hi guy's...

I'm totally lost here..because don't have any idea how to make a query for grab r

GET vs SUBMIT Button to show sections on one index.php page?
Just curious if one of these ways is better (i.e. more efficient) than the other.

I have a

Product categories for registration
Dear all,

We are going live with the Supplier registered next week. At standard, the 'sel

paginate search result
Hi, I have a paginations script to display data from my database but i would like to paginate someon

urldecode question
How would I format this line of code properly?

<?php echo urldecode($_GET['Title']); ?

Help with file_get_html
The following code gives the expected results:

<html>
<head&g

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