Bug #79592 [Com]: Converting string to bool when "0" passed

From: Date: Wed, 13 May 2020 15:05:56 +0000
Subject: Bug #79592 [Com]: Converting string to bool when "0" passed
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227025@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 Comment by: bugreports at gmail dot com Reported by: 6562680 at gmail dot com Summary: Converting string to bool when "0" passed 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: come on and fix your expectations given nobody but you expects "0" and 0 which is practically the same when it comes to type juggling evaluate to true 0/1 is the same as true/false, with or without you when it comes too lose types Previous Comments: ------------------------------------------------------------------------ [2020-05-13 14:00:28] 6562680 at gmail dot com Damn, $var = '0'; empty($var); // true Even there... ------------------------------------------------------------------------ [2020-05-13 13:58:56] 6562680 at gmail dot com You know, guys, documentation is like exception - it is delayed uncoded solution for the problem. Good application never thrown exception. But as one coder you cant hold in your mind all possible exceptions so you throw it for future fixing. The community imagine "documentation" word like "we just write there is a bug and it is not a bug. we're done" ------------------------------------------------------------------------ [2020-05-12 21:18:46] girgias@php.net 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). ------------------------------------------------------------------------ [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

« previous php.bugs (#227025) next »