mardi 24 février 2015

PHP How can you use string instead of == or != in IF statement

I wonder how i can make if work if i put == or != in variable e.g.



$comparison_operator = " != ";

if ( 5 $comparison_operator 5 ) {
echo "true";
} else {
echo "false";
}


i tried with eval() however only works when you put entire expression in it not just variable.


This works



$comparison_operator = " != ";

if ( eval('return(5 $comparison_operator 5);') ) {
echo "true";
} else {
echo "false";
}


This does not and i need version that looks like this to work somehow, i tried variable variables without luck as well, any ideas?



$comparison_operator = " != ";

if ( 5 eval('return($comparison_operator)') 5 ) {
echo "true";
} else {
echo "false";
}

Aucun commentaire:

Enregistrer un commentaire