Req #49426 [Com]: Logic error in boolean cast from string

From: Date: Mon, 19 Dec 2016 09:03:52 +0000
Subject: Req #49426 [Com]: Logic error in boolean cast from string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206123@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49426&edit=1

 ID:                 49426
 Comment by:         miklcct at gmail dot com
 Reported by:        az at kreatiefer dot de
 Summary:            Logic error in boolean cast from string
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        *
 Block user comment: N
 Private report:     N

 New Comment:

This design in PHP is very counter-intuitive and error-prone.

I think when converting from string to boolean, any string, except empty string, should be converted
to true, including all of arrTestVars below.


Previous Comments:
------------------------------------------------------------------------
[2009-08-31 17:49:29] az at kreatiefer dot de

Description:
------------
When you cast a string variable to boolean type there are some cases in which the cast doesn't
work properly, i think.

Reproduce code:
---------------
$arrTestVars = array(
  '0',
  '-0',
  '+0',
  '0.0',
  '0e0',
  '0.0e0'
);

foreach($arrTestVars as $strTestVar) {
  var_dump(
    (bool)$strTestVar
  );
}


Expected result:
----------------
// Expected Result => (bool)false, (bool)false, (bool)false, (bool)false, (bool)false,
(bool)false

All the tests above should be evaluated to false, because
(bool)0.0 === false
(bool)0e0 === false
and so on.

Now one has to use the workaround (bool)(int)'0.0' === false ...


Actual result:
--------------
// Actual Result => (bool)false, (bool)true, (bool)true, (bool)true, (bool)true, (bool)true


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=49426&edit=1


Thread (3 messages)

« previous php.bugs (#206123) next »