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

wav to mp3 + goDaddy
I need to convert wav files to mp3s. From what I have found so far one way to do it is to have my PH

check if string contain only a-z/A-Z
Is where any way to check if string contains only a-z/A-Z, without writing an array with all possibl

How do I use ValidatorCallout extender in Login control?
Hi,I'd like to customize the look and feel of the login control a little bit. One thing I'd love to

How can I Compare two xml documents?
Hi all,

I am doing a POC for my project and I am using XE database 10g version.

utf8_general_ci error ??
I have all kinds of data to be inserted in database table

//I am getting this error

Yo

Php - mysql store data and use it later?
Hi, i' making a login page at the moment, however my username + password is stored many different pl

Please help with code for SMTP authenticated PHP email form
Hello, I'm creating a PHP email form, and for this particular server, I have to use SMTP Authenticat

Typing math
I've added support for typesetting math using LaTeX on the forums.

Example:
Code: [Select]

Custom array sort? asc then desc
Any ideas how I could sort this array? I've been trying for a while. Maybe with usort, but I have no

Modal Popup Help
Hi guys, I have a modal popup with an iframe. How can i send data from a label or textbox to the ifr

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