1/** filter_var - Filters a variable with a specific filter **/
2$boolvar = filter_var('true', FILTER_VALIDATE_BOOLEAN);
3/** boolval - Get the boolean value of a variable PHP 5 >= */
4$boolvar = boolval ('true');
5// And literally with a ternary operator but I can't recommend it
6$boolvar = ($string === 'true') ? true: false;
7
8/** We can convert any variable to boolean
9--- using the (bool) or (boolean) keyword -----
10*** But we are talking about conversion not casting ***
11- So any String whose length is greater than 0 is true
12- and any number other than 0 is true
13**/
14echo $boolvar = (bool)1; //return true
15echo $boolvar = (bool)"true"; //return true
16echo $boolvar = (bool)"false"; //return true
17echo $boolvar = (bool)0; //return false
18echo $boolvar = (bool)""; //return false
19
20var_dump((bool) 0);//return false
21var_dump((bool) "");//return false
1/**
2 * Strings always evaluate to boolean true unless they have a
3 * value that's considered "empty" by PHP (taken from the
4 * documentation for empty):
5 * "" (an empty string) evaluates as false.
6 * "0" (0 as a string) evaulates as false.
7 * If you need to set a boolean based on the text value of a
8 * string, then you'll need to check for the presence or
9 * otherwise of that value.
10 */
11$boolean = $string === 'true' ? true: false;
1// (PHP 5 >= 5.5.0, PHP 7)
2// boolval — Get the boolean value of a variable
3boolval ( mixed $var ) : bool
4// Returns the boolean value of var.