简体   繁体   English

如何在PHP中将字符串转换为boolean多个条件

[英]How to convert string to boolean multiple condition in PHP

How to convert string to conditional boolean如何将字符串转换为条件boolean

example string with boolean condition.条件为 boolean 的示例字符串。

$condition1 = 'false || ( true && false)'; // should return false
$condition2 = 'false || true';  // should return true
$condition3 = 'true && false';  // should return false
$condition4 = 'true && (true || false);  // should return true

I tried these codes but not working..我试过这些代码但没有用..

$result =  (boolean) $condition1;
$result = filter_var($condition1, FILTER_VALIDATE_BOOLEAN);
$result = settype($condition1, 'boolean');

is there a way to convert it?有没有办法转换它?

You can use php eval() function您可以使用php eval() function

eval("\$status=true || false; return \$status;")

In your case在你的情况下

eval('\$condition1 = false || ( true && false);');
eval('\$condition2 = false || true;');
// And so on ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM