Bug #51657 [Com]: switch() construction works wrong with empty arrays
| From: | 1234ru at gmail dot com | Date: | Tue, 27 Apr 2010 17:43:03 +0000 |
| Subject: | Bug #51657 [Com]: switch() construction works wrong with empty arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-149966@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51657&edit=1
ID: 51657
Comment by: 1234ru at gmail dot com
Reported by: 1234ru at gmail dot com
Summary: switch() construction works wrong with empty arrays
Status: Bogus
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.2.13
New Comment:
Now I see I was using language construction in a wrong way (in fact, I
was insensibily hacking switch).
Thanks a lot for explanaion!
Previous Comments:
------------------------------------------------------------------------
[2010-04-27 11:26:16] degeberg@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
While perhaps not immediately clear, this is correct behavior.
An empty array evaluates to false and in your snippet, isset($a['a'])
evaluates to false. In other words, $a == isset($a['a']), and so the
first case in your switch will be used.
------------------------------------------------------------------------
[2010-04-25 00:11:41] 1234ru at gmail dot com
Description:
------------
When building switch() structure using arrays, if array is empty, first
case always fires (while it should not)
(version 5.2.12)
Test script:
---------------
$a = array();
switch($a) {
case (isset($a['a']) ): echo 'If $a is empty, you should not see it';
break;
default: echo 'Hello.'; break;
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51657&edit=1