<?php
function toBool($var) {
if (!is_string($var)) return (bool) $var;
switch (strtolower($var)) {
case '1':
case 'true':
case 'on':
case 'yes':
case 'y':
return true;
default:
return false;
}
}
$strs = ['1', '0', 'true', 'false', 'on', 'off', 'yes', 'no', 'y', 'n'];
foreach ($strs as $str) {
echo "toBool($str) : " . (toBool($str) ? 'true' : 'false') . "\n";
echo "filter_var($str) : " . (filter_var($str, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ? 'true' : 'false') . "\n";
echo "parse_ini_string($str): " . (parse_ini_string("val = $str")['val'] ? 'true' : 'false') . "\n";
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- toBool(1) : true
filter_var(1) : true
parse_ini_string(1): true
toBool(0) : false
filter_var(0) : false
parse_ini_string(0): false
toBool(true) : true
filter_var(true) : true
parse_ini_string(true): true
toBool(false) : false
filter_var(false) : false
parse_ini_string(false): false
toBool(on) : true
filter_var(on) : true
parse_ini_string(on): true
toBool(off) : false
filter_var(off) : false
parse_ini_string(off): false
toBool(yes) : true
filter_var(yes) : true
parse_ini_string(yes): true
toBool(no) : false
filter_var(no) : false
parse_ini_string(no): false
toBool(y) : true
filter_var(y) : false
parse_ini_string(y): true
toBool(n) : false
filter_var(n) : false
parse_ini_string(n): true
preferences:
99.25 ms | 410 KiB | 5 Q