Avatar ·

How to convert a string to boolean in PHP?

Hello everyone! I just started learning PHP and ran into a problem. I have a variable that comes from a form or from a database as a string. For example, it could be "true", "false", "1", "0", "yes", "no". I need to convert this string to a real boolean type (bool) to use in conditions.\n\nI tried just assigning the value: `$flag = "true";` — but this is a string, not a boolean. Then I tried casting with `(bool) $string` — but this returns `true` for any non-empty string, even for "false". That is, `(bool) "false"` returns `true`, which is completely not what I need.\n\nHere is an example of code that does not work as expected:\n\n```php\n$stringValue = "false";\n$boolValue = (bool) $stringValue; // Result: true, but should be false\nvar_dump($boolValue); // bool(true)\n```