Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to assign eval(0 to variable

<?php  
    $xs = eval("if ('1' == '0')
                echo 'never';
            else
                echo 'always';");

    //echo $xs;

This code returns 'always' but i don't want it. I need to take this variable elsewhere.

Sorry for bad english.

EDIT:

PEOPLE!!!!!!!This sample code. I know that eval() in this case is not needed, but the code in my other projects will be. I need to what eval() returns were entered into a variable. "

like image 301
Isis Avatar asked Dec 13 '22 15:12

Isis


2 Answers

Here's a simple way to get the result:

<?php  
ob_start();
eval("if ('1' == '0')
            echo 'never';
        else
            echo 'always';");
$xs = ob_get_contents();
ob_end_clean();
//echo $xs;

To get the return value of eval() you'll need to return something inside the evaluated code, e.g.:

$xs = eval("return '1' == '0' ? 'never' : 'always';");
echo $xs; // echoes 'always'
like image 165
jmz Avatar answered Dec 27 '22 19:12

jmz


$xs = ('1' == '0') ? 'never' : 'always';
like image 36
tsadiq Avatar answered Dec 27 '22 18:12

tsadiq