user1885099
user1885099

Reputation: 150

dynamically generated if statement eval

This is my code:

$val1 = 0; $val2 = 3; $cond = '>';

$check = $val1.$cond.$val2;

echo eval('return $check;') ? 'true' : 'false';

I have a function that, using received parameters, is generating dynamic if statements.

All the statements are generated without errors.

The above code is an example of the code used in said function.

The problem is that all statements return TRUE.

I am sure that I am using eval in a wrong way, but I cannot figure out how.

Upvotes: 0

Views: 59

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31173

If you use single quotes the text is interpreted as is, which means you are evaluating return $check. You want to use double quotes which means you're evaluating return 0>3.

So use echo eval("return $check;") ? 'true' : 'false';.

Upvotes: 1

Related Questions