PHP bool Expression

0

I'm working on a boolean expression configured by the application user. This expression comes from the database as soon as it's a string, I need to make PHP understand it so I have both true and false.

Example:

Expressao_string = ”(1 and ( 0 or 1) )”;

As this string value comes from a base, I need to make this string valid for PHP.

    
asked by anonymous 28.09.2017 / 11:38

1 answer

0

If I understand your question well, the bank is giving you 0 and 1 for Booleans and you want to convert this from string to boolean in php, right?

You do not have to treat anything, php already auto-converts this, see this test I did:

<?php

testar('0');
testar('1');
testar(0);
testar(1);

function testar($testeBool){
    if ($testeBool == true){
        echo "teste bem sucedido";
    }
    else{
        echo "temos um problema";
    }
}

?>

The outputs were:

testar('0'); //temos um problema
testar('1'); //teste bem sucedido
testar(0); //temos um problema
testar(1); //teste bem sucedido

See working at Ideone .

    
28.09.2017 / 12:13