I have tried many different combinations of code using the eval command. However my answer is always blank. Here is a simple one i tried.
$equation = "18 - (18 * 2) - 1";
$var = eval($equation);
echo "answer: " . $var;
I understand this does not need the eval code, but the real code I am working on does. It just always shows up blank.
Solution
Add
returninside the evaluated statement, otherwise theeval()won’t know what to return:Proof, that the above works: http://ideone.com/bTtIH
About superiority of
returnvs. setting variableMore from the documentation of
eval():In other words: there is no need to pollute the namespace, you can get what you want directly from what
eval()returns.