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 do not want this. I need to take this variable elsewhere.

Sorry for the bad english.

EDIT:

PEOPLE!!!!!!! This sample code. I know that eval () is not required in this case, but there will be code in my other projects. I need the eval () variables to be introduced into the variable. "

+4
source share
6 answers

Here is an easy 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 need to return something inside the evaluated code, for example:

 $xs = eval("return '1' == '0' ? 'never' : 'always';"); echo $xs; // echoes 'always' 
+8
source
 $xs = ('1' == '0') ? 'never' : 'always'; 
+1
source

this code doesn't make sense, but eval sucks :-)

try something like this:

 $xs = '1' == '0' ? 'never' : 'always'; echo $xs; 
+1
source
 $xs = ('1' == '0') ? 'never' : 'always'; 

This is an alternative structure that you can also use in a string, since you get parentheses around it :)

+1
source

Do not use eval!

  function test ()
 {
     return '1' == '0'?  'never': 'always' ;;
 }

 echo test ();
0
source

Assuming you have a very good reason to use eval, you can use one of your create_function monsters.

 $x2 = create_function( '$a', 'echo $a == 1 ? "yes" : "no";' ); $x2( 1 ); // echos 'yes' 
0
source

Source: https://habr.com/ru/post/1342808/


All Articles