Bug #80490 [Nab]: Switch choose a wrong block, if int(0) is a value

From: Date: Mon, 07 Dec 2020 13:04:37 +0000
Subject: Bug #80490 [Nab]: Switch choose a wrong block, if int(0) is a value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230898@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80490&edit=1 ID: 80490 Updated by: dharman@php.net Reported by: miso at fykos dot cz Summary: Switch choose a wrong block, if int(0) is a value Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: ubuntu 18.04 PHP Version: 7.4.13 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php You are using switch statement which performs loose comparison. Up to PHP 7.4 the type system was treating 0 == 'a'. Since PHP 8 the type system and loose comparisons have been improved. The behaviour you are seeing is expected in PHP 7.4. See it online here: https://3v4l.org/lKu3p The following example might be also helpful to see how to comparison is performed: https://3v4l.org/rmTmq If you are already on PHP 8, you can avail of "match" statement which performs a strict comparison, but works in a similar manner to switch However, it looks like you would benefit more from an if statement instead. $key = 0; if('a' === $key) { echo 'break'; } else { echo 'default'; } If you want to use switch and perform strict comparison, then you can abuse the structure and do the following: switch (true) { case 'a' === $key: echo 'break'; break; default: echo 'default'; break; } But it would be more appropriate to use an if statement in this case. Previous Comments: ------------------------------------------------------------------------ [2020-12-07 12:56:28] peehaa@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php In particular: https://www.php.net/manual/en/control-structures.switch.php and https://www.php.net/manual/en/language.types.type-juggling.php and ------------------------------------------------------------------------ [2020-12-07 12:52:59] rjrjdjdu at dfjdrj dot com learn about type juggeling - "a" casted to int is 0 ------------------------------------------------------------------------ [2020-12-07 12:08:35] miso at fykos dot cz Description: ------------ When argument of switch is a int(0), switch choose a wrong statement. Test script: --------------- <?php $key = 0; switch ($key) { case 'a': echo 'break'; break; default: echo 'default'; break; } Expected result: ---------------- 'default' Actual result: -------------- 'break' ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80490&edit=1

« previous php.bugs (#230898) next »