Search code examples
phpoperator-keywordoperands

Can I use operator in variable


Can I write code like below in php?

$operator = ">=90";
$val = 100;
if($val.$operator){
echo "Correct!";
}

Solution

  • Look into the eval() function. Though, I would say you should look into closures and even arrow functions.

    $operator = fn($x) => $x >= 90;
    $val = 100;
    if($operator($val)){
        echo "Correct!";
    }