How can I convert string to boolean?
$string = 'false';
$test_mode_mail = settype($string, 'boolean');
var_dump($test_mode_mail);
if($test_mode_mail) echo 'test mode is on.';
it returns,
boolean true
but it should be boolean false.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strings always evaluate to boolean true unless they have a value that’s considered “empty” by PHP (taken from the documentation for
empty):""(an empty string);"0"(0 as a string)If you need to set a boolean based on the text value of a string, then you’ll need to check for the presence or otherwise of that value.
EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:
or maybe use of the
filter_varfunction may cover more boolean values:filter_varcovers a whole range of values, including the truthy values"true","1","yes"and"on". See here for more details.