Is there a way to get "true" / "false" string values ​​from Boolean in PHP?

When I use Boolean (using (bool) ), there is a built-in way to make PHP actually return true or false constants. At the moment, I get 1 or a space, which is respectively calculated as true and false.

I need to return the value for clearer semantics. However, if I cannot get it, I just agree with 1 and empty.

+3
source share
3 answers

PHP displays boolean values ​​as 1 (true) or an empty string (false) in the output.

If you want to check if it uses true or false == (if the implicit conversion is ok) or === (if it isn't). For instance:

 echo $val ? 'true' : 'false'; // implicit conversion echo $val === true ? 'true' : 'false'; // no conversion 

I do not know how to make PHP output booleans initially as true or false .

+4
source

If you are too lazy to perform a comparison and echo is a string, or if you just want to shorten it, you can use:

 var_export($boolean, true); // the second parameter is to return and not output 

PHP: var_export

+7
source

If you are looking for the lines "true" and "false", a three-dimensional condition would be ideal:

 <?=(($boolean) ? "true" : "false")?> 
0
source

Source: https://habr.com/ru/post/921269/


All Articles