Bob J
Bob J

Reputation: 333

php eval() error after updating

I cant seem to find the error in here, this code used to work then I updated PHP and now I get :

Parse error: syntax error, unexpected '10' (T_LNUMBER) in C:\wamp\www\a\1.php(15) : eval()'d code on line 1

$operande1 = 5;
$operande2 = 10;
$operation = "*";
calcul($operande1,$operande2,$operation);
function calcul($operande1, $operande2, $operation) {
                echo $operande1;
                echo $operande2;
                echo $operation;
                eval('$result=('.$operande1.")".$operation."(".$operande2.");");
}

Any help is appreciated

Upvotes: 0

Views: 625

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

The only way I was able to reproduce the error that you posted

Parse error: syntax error, unexpected '10' (T_LNUMBER)

was to leave out the = sign.

$operande1  10;

which gave me

Parse error: syntax error, unexpected '10' (T_LNUMBER)


http://3v4l.org/o1vDg

Output for 5.4.0 - 5.6.2, php7@20140507 - 20141001 Parse error: syntax error, unexpected '10' (T_LNUMBER) in /in/o1vDg on line 3

Upvotes: 0

Juan Cortés
Juan Cortés

Reputation: 21082

You're concatenating a string with a number in the eval. Wrapping the $operande1 inside strval($operande1) should solve this. I don't recommend using eval at all but, it would look like this, another option is to simply have the numbers as strings, by initializing them inside quotation marks ie $operande1 = "10";

eval('$result=('.strval($operande1).")".$operation."(".strval($operande2).");");

Note that the eval is just setting the value to the variable $result, and you'll to do echo $result; to print its value.

Upvotes: 1

Related Questions