note 90037 added to language.types.boolean

From: Date: Thu, 02 Apr 2009 14:27:18 +0000
Subject: note 90037 added to language.types.boolean
Groups: php.notes 
Request: Send a blank email to php-notes+get-152765@lists.php.net to get a copy of this message
PHP is very fussy converting strings to booleans. The only ones it recognizes are '0' or '', everything else evaluates to TRUE, even 'false' and '0.0' are evaluated as true! I suppose this can't be fixed without breaking a lot of existing code. Example: <?php print 'yes'."\t".((bool)'yes'? 1: 0)."\n"; print 'true'."\t".((bool)'true'? 1: 0)."\n"; print 'no'."\t".((bool)'no'? 1: 0)."\n"; print 'false'."\t".((bool)'false'? 1: 0)."\n"; print '1'."\t".((bool)'1'? 1: 0)."\n"; print '0'."\t".((bool)'0'? 1: 0)."\n"; print '0.0'."\t".((bool)'0.0'? 1: 0)."\n"; print ''."\t".((bool)''? 1: 0)."\n"; ?> Output: yes 1 true 1 no 1 false 1 1 1 0 0 0.0 1 0 ---- Server IP: 66.207.199.35 Probable Submitter: 199.243.246.66 ---- Manual Page -- http://www.php.net/manual/en/language.types.boolean.php Edit -- https://master.php.net/note/edit/90037 Del: integrated -- https://master.php.net/note/delete/90037/integrated Del: useless -- https://master.php.net/note/delete/90037/useless Del: bad code -- https://master.php.net/note/delete/90037/bad+code Del: spam -- https://master.php.net/note/delete/90037/spam Del: non-english -- https://master.php.net/note/delete/90037/non-english Del: in docs -- https://master.php.net/note/delete/90037/in+docs Del: other reasons-- https://master.php.net/note/delete/90037 Reject -- https://master.php.net/note/reject/90037 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152765) next »