Skip to content

Instantly share code, notes, and snippets.

@df2k2
Last active August 27, 2020 13:31
Show Gist options
  • Save df2k2/b9f0e8bebf0f47494d0cab2743ac310e to your computer and use it in GitHub Desktop.
Save df2k2/b9f0e8bebf0f47494d0cab2743ac310e to your computer and use it in GitHub Desktop.
PHP String converted to boolean
<?php
$stringToBool = (preg_match('/yes|true|(?<!-)[1-9]/i', $value)) ?: 0;
$value = ["yes","1","true","0","false","no",1,"NO","No","YES","Yes",false,true,"no",-1,0,6 ];
foreach ($value as $v) {
echo "$v is " . ((preg_match('/yes|true|(?<!\-)[1-9]/i', $v)) ?: 0) . "<br>";
}
/**
yes is 1
1 is 1
true is 1
0 is 0
false is 0
no is 0
1 is 1
NO is 0
No is 0
YES is 1
Yes is 1
is 0 // (bool) false is 0
1 is 1 // (bool) true is 1
no is 0
-1 is 0
0 is 0
6 is 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment