Bug #79592 [Opn->Nab]: Converting string to bool when "0" passed
| From: | girgias@php.net | Date: | Tue, 12 May 2020 21:18:46 +0000 |
| Subject: | Bug #79592 [Opn->Nab]: Converting string to bool when "0" passed | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227020@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79592&edit=1
ID: 79592
Updated by: girgias@php.net
Reported by: 6562680 at gmail dot com
Summary: Converting string to bool when "0" passed
-Status: Open
+Status: Not a bug
Type: Bug
Package: Filter related
Operating System: Win10
PHP Version: 7.3.17
Block user comment: N
Private report: N
New Comment:
This is documented behaviour, if you want a reason why this behaves like this imagine it being
converted to an integer first and then converted to a boolean (because in effect a boolean is just
an integer that is 0 for false and anything else for true).
Previous Comments:
------------------------------------------------------------------------
[2020-05-12 21:14:32] 6562680 at gmail dot com
Description:
------------
I know, i know...
"you should use strict_types()" and so...
But really guys, why, exactly, why string "0" will be converted to false while calling
(bool) ?
Please :///
Once i found same joke while comparing two values on usort(), fixed... now i remember used
everywhere (! $string) and confused... little bit.
Test script:
---------------
function (string $name) {
if (! $name) {
// nice one, junior, you'll lose "0" right here
// please rewrite your project and use ('' !== $name) or be fired
throw new \InvalidArgumentException('String should be not empty');
}
// some code
}
Expected result:
----------------
(bool) 0 === false
(bool) "0" === true
(bool) (string) 0 === true
...
Actual result:
--------------
(bool) 0 === false
(bool) "0" === false
(bool) (string) 0 === false
...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79592&edit=1