php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49097 Boolean type casting print problem
Submitted: 2009-07-29 10:31 UTC Modified: 2009-07-29 12:48 UTC
From: mkrasuski at infico dot pl Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.3.0 OS: Windows 7/XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mkrasuski at infico dot pl
New email:
PHP Version: OS:

 

 [2009-07-29 10:31 UTC] mkrasuski at infico dot pl
Description:
------------
I think, there's a problem with type casting -- to be more specific, with boolean casting.

When I was trying to print result of the condition (should be true, or false) I received some unexpected output.

Reproduce code:
---------------
<?php

$b = 2;

echo 'Should be false: ' . (!$b);
echo '<br />';
echo (string) !$b;
echo '<br />';
echo (bool) !$b;
echo '<br />';
echo (int) !$b;
echo '<br />';
echo 'Should be true: ' . ((bool) $b);
echo '<br />';
var_dump(!$b);

?>

Expected result:
----------------
Should be false: false
false  // or 0?
false
0
Should be true: true
bool(false)

Actual result:
--------------
Should be false: 


0
Should be true: 1
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-29 11:38 UTC] scottmac@php.net
All works here, the cast of a bool to string is "".

<?php

$b = 2;

var_dump((!$b));
echo PHP_EOL;
var_dump((string) !$b);
echo PHP_EOL;
var_dump((bool) !$b);
echo PHP_EOL;
var_dump((int) !$b);
echo PHP_EOL;
var_dump(((bool) $b));
echo PHP_EOL;
var_dump(!$b);

Output:
bool(false)

string(0) ""

bool(false)

int(0)

bool(true)

bool(false)

 [2009-07-29 11:51 UTC] 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".)
 [2009-07-29 11:56 UTC] rasmus@php.net
false has to be an empty string to correctly evaluate to false.  No bug here.
 [2009-07-29 12:24 UTC] mkrasuski at infico dot pl
Ok about string. But what about casting to bool (same problem).)
 [2009-07-29 12:33 UTC] 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:37 UTC] 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:48 UTC] mkrasuski at infico dot pl
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"? ;))
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jul 04 06:05:45 2022 UTC