Skip to content
PHP Logo

How does PHP handle the conversion of certain string values like "on" and "off" to boolean true and false?

PHP automatically converts certain string values like "on", "true", "yes", and "1" to boolean true, and values like "off", "false", "no", and "0" to boolean false. This automatic type conversion can be useful when working with form inputs or configuration settings that use these common string representations of boolean values.

php
$input = "on";
$boolValue = filter_var($input, FILTER_VALIDATE_BOOLEAN);
var_dump($boolValue); // Output: bool(true)