简体   繁体   中英

How to convert string to boolean multiple condition in PHP

How to convert string to conditional boolean

example string with boolean condition.

$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

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

In your case

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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