使用数字评估连续运算符

I'm struggling with a small piece of code that doesn't want to evaluate itself :

$t = 5;
$s = "<=";
$r = 6;

var_dump($t.$s.$r);

Here the var_dump return "5<=6" which make sense but I just want it to tell me if 5 is inferior or equal to 6 with a boolean.

I wanted to know if there was an other way to get this boolean beside using an eval() or a switch throught all the possible operator

Thanks in advance.

If you want a safe and flexible solution, this allows you to define a method which is executed depending on the operator matching the key in an array, it only works with two operands, but the last one in the examples # just multiplies the first value by 4 and returns the value...

$operators = [ "<=" => function ($a, $b) { return $a <= $b;},
    "<" => function ($a, $b) { return $a < $b;},
    ">=" => function ($a, $b) { return $a >= $b;},
    ">" => function ($a, $b) { return $a > $b;},
    "#" => function ($a) { return $a * 4; }];

$t = 5;
$s = "<=";
$r = 6;
var_dump($operators[$s]($t,$r));

$s = "<";
var_dump($operators[$s]($t,$r));

$s = ">=";
var_dump($operators[$s]($t,$r));

$s = ">";
var_dump($operators[$s]($t,$r));

$s = "#";
var_dump($operators[$s]($t,$r));

gives...

/home/nigel/workspace2/Test/t1.php:14:
bool(true)
/home/nigel/workspace2/Test/t1.php:17:
bool(true)
/home/nigel/workspace2/Test/t1.php:20:
bool(false)
/home/nigel/workspace2/Test/t1.php:23:
bool(false)
/home/nigel/workspace2/Test/t1.php:26:
int(20)

It's a bit convoluted, but also extensible and safe.

while it is generally not a good idea to have code usch as this (evaluating code that is sstored as plaintext), there is a function for exactly this: eval().

eval() does what you expect PHP to do naturally: evaluate valid code stored in a string.

eval("var_dump(".$t.$s.$r.");"); will do the job - however, since any code inside those variables is executed without question, it can be a security risk, or at least introduce some hard-to-debug errors. (the extra quoting and the ; are needed to make the code inside eval actually valid PHP code)