0

I use eval('1+1') without any issue in JavaScript but for some reason I can't get this to work in PHP. This is an internal (non-public) webpage so security is not an issue. No matter how I code it I am seeing an 500 Internal Server Error.

I'm trying to do something like this in a PHP file:

$expression='1+1';

echo "eval($expression)";

Is the eval() function in PHP not the same as the JavaScript equivalent or is it perhaps not as straightforward to implement?

Ken Boone
  • 143
  • 2
  • 9

6 Answers6

1

You have the following code:

$expression='1+1';
echo "eval($expression)";

Since you're quoting the eval() statement, it'd just print eval(1+1) literally.

eval() needs a valid expression.

You're probably looking for:

$expression='1+1'; 
echo eval("echo $expression;");

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    Ah, you beat me before I could fix mine. I knew mine didn't look right. –  Oct 19 '13 at 07:55
  • I'm not clear why the code: eval("echo $expression;"); actually outputs the results (as opposed to storing it in a variable). That being said, is there a way to store the result instead of echoing it? – Ken Boone Oct 23 '13 at 03:36
  • @KenBoone: `eval()` executes arbitrary PHP code. In this case, the code is `echo $expression`. `$expression` will have the output of `1+1` and the code will just print out 2. If you want to store the result in a variable, then you can do something like this: `eval("\$var = $expression;");` -- this will store the result in a variable and you can re-use it later, if you want to. See a demo here: https://eval.in/56676 (**don't** use `eval()` if it involves user input somewhere. It's [**dangerous**](http://stackoverflow.com/q/951373/1438393).) – Amal Murali Oct 23 '13 at 12:03
1

eval() function must have as argument a valid php code, see this example:

echo eval("return 1+1;");
RobotMan
  • 488
  • 4
  • 15
0

http://php.net/manual/en/function.eval.php

Note that the eval() function is very dangerous, regardless of internal server or not, be careful.

Now, eval() in PHP allows executing of PHP code. What you're doing is echoing executing PHP code.

That said, try this:

$expression = "1+1";
echo eval("echo $expression;");
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

eval() executes PHP code, not just about anything like any arbitrary mathematical expression. 1+1 is not a valid PHP code expression. So give it proper PHP expression to execute. like:

$expression='echo 1+1;'; 
eval($expression);

Valid PHP code to be evaluated.

Reference

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

Try this,

eval('$expression =1+1;');
echo $expression;
Pradeep shyam
  • 1,282
  • 16
  • 27
-1
$expression = "1+1";

$result = " return (".$expression.")
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
  • 4
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Aug 10 '22 at 20:10
  • 1
    The code won't evaluate anything. Did you try to run it before posting it? – Nico Haase Aug 11 '22 at 12:33