note 47068 added to language.types.boolean
| From: | Jared at osu1 dot php dot net | Date: | Mon, 01 Nov 2004 23:37:03 +0000 |
| Subject: | note 47068 added to language.types.boolean | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79472@lists.php.net to get a copy of this message | ||
Discord's method is ok. But a bit bulky.
I wanted to use (bool) to ensure a field going into MySQL wasn't any greater than 1, and not
less than 0. That obviously isn't going to work too great with (bool).
So I found this nice method that can be used for other things as well.
$var = "false"
$var = ($var) ? 1 : 0;
Now strings can be bool'ed. 1, "1", "true", true, 2, -1, etc will end up
being 1. Anything that equalled 0 (0, "0", false, "false"), will return a 0.
Works for me! Now you'll get a 0 or 1, nothing else.
----
Manual Page -- http://www.php.net/manual/en/language.types.boolean.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47068
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47068&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47068&report=yes
Search -- http://master.php.net/manage/user-notes.php