note 31830 deleted from language.types.boolean by betz
| From: | betz@php.net | Date: | Sun, 03 Jul 2005 08:15:46 +0000 |
| Subject: | note 31830 deleted from language.types.boolean by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-91339@lists.php.net to get a copy of this message | ||
Note Submitter: discord at spambegone dot mac dot com
----
/* Automatic boolean conversion in php is in line
with most other programming languages. However,
sometimes you may need more flexiblity with the
type conversion. Here's an easy way:
Say you want the user to be able to type
any affirmative or negative and have it
recognised as a boolean... you might do this:
*/
$bool = strtolower($_GET['user_response']);
$bool = ($bool=='yes' or $bool=='true' or $bool=='correct'
or $bool=='on' or $bool=='right' or $bool=='positive'
or $bool=='yup' or $bool=='uh-huh' or $bool=='sure'
or $bool===true);
/* this will set it to true if any of these strings
are entered, or if it's already true; otherwise,
it converts it to false. Note the triple equal sign
when comparing it to the unquoted boolean true:
this is the "exactly equal to" operator, which
checks not just for equality, but for type. Using it
ensures that $bool doesn't get converted to a boolean
for the comparison: $bool===true is only true
if $bool is a true boolean, whereas $bool==true
is true for any non-empty string (except "0").
*/