#49097 [Bgs]: Boolean type casting print problem

From: Date: Wed, 29 Jul 2009 12:48:43 +0000
Subject: #49097 [Bgs]: Boolean type casting print problem
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-140438@lists.php.net to get a copy of this message
ID: 49097 User updated by: mkrasuski at infico dot pl Reported By: mkrasuski at infico dot pl Status: Bogus Bug Type: Output Control Operating System: Windows 7/XP PHP Version: 5.3.0 New Comment: Hi again, > $false = false; > if ((string)$false)) { die("oops"); } > If the typecast made it return "false" > then you'd see the oops here. You gave too abstract example. Personally I haven't seen such construction. Most people, IMO, would rather do: $string = ''; if( !((bool) $string) ) { die("oops"); } Besides, I think, it's against the programming convention :) > You need to be able to get consistent > behavior regardless of the cast Well... doing (string)((int)((string) '153 foo')); you won't get '153 foo' as the result. So where's the "consistent behavior"? ;) Previous Comments: ------------------------------------------------------------------------ [2009-07-29 12:37:45] scottmac@php.net You need to be able to get consistent behavior regardless of the cast, not everyone is just printing the output. $false = false; if ((string)$false)) { die("oops"); } If the typecast made it return "false" then you'd see the oops here. ------------------------------------------------------------------------ [2009-07-29 12:33:19] mkrasuski at infico dot pl One more thing. > false has to be an empty string to correctly evaluate to false. > No bug here. May I ask, why? When casting '153 foo' to int you get 153. Why, when casting 'false' (or 'FALSE', whatever) you get 1 (true), not 0 (false)? PHP is a script language, it should the difference, IMO.) ------------------------------------------------------------------------ [2009-07-29 12:24:15] mkrasuski at infico dot pl Ok about string. But what about casting to bool (same problem).) ------------------------------------------------------------------------ [2009-07-29 11:56:20] rasmus@php.net false has to be an empty string to correctly evaluate to false. No bug here. ------------------------------------------------------------------------ [2009-07-29 11:51:24] mkrasuski at infico dot pl You missunderstood me. The cast of a bool(false) is "" but cast of a bool(true) is "1". IMO it should be "false" and "true", or, at least "0" and "1".) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/49097 -- Edit this bug report at http://bugs.php.net/?id=49097&edit=1

« previous php.bugs (#140438) next »