How to parse a string of boolean logic in PHP

后端 未结 3 1730
醉酒成梦
醉酒成梦 2020-12-28 09:12

I\'m building a PHP class with a private member function that returns a string value such as:

\'true && true || false\'

to a public

3条回答
  •  星月不相逢
    2020-12-28 09:45

    eval() will work perfectly fine for this, but remember you have to tell it to return something.

    $string = "true && true || false";
    $result = eval("return (".$string.");"); // $result will be true
    

    Also make sure you sanitize any user inputs if putting them directly into an eval().

提交回复
热议问题