note 31830 added to language.types.boolean

From: Date: Mon, 05 May 2003 17:37:27 +0000
Subject: note 31830 added to language.types.boolean
Groups: php.notes 
Request: Send a blank email to php-notes+get-47551@lists.php.net to get a copy of this message
/* 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"). */ -- http://www.php.net/manual/en/language.types.boolean.php http://master.php.net/manage/user-notes.php?action=edit+31830 http://master.php.net/manage/user-notes.php?action=delete+31830 http://master.php.net/manage/user-notes.php?action=reject+31830

« previous php.notes (#47551) next »