php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51657 switch() construction works wrong with empty arrays
Submitted: 2010-04-25 00:11 UTC Modified: 2010-04-27 11:26 UTC
From: 1234ru at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.13 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: 1234ru at gmail dot com
New email:
PHP Version: OS:

 

 [2010-04-25 00:11 UTC] 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;
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-25 19:23 UTC] felipe@php.net
-Package: Arrays related +Package: Scripting Engine problem
 [2010-04-27 11:26 UTC] degeberg@php.net
-Status: Open +Status: Bogus
 [2010-04-27 11:26 UTC] 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-27 19:43 UTC] 1234ru at gmail dot com
Now I see I was using language construction in a wrong way (in fact, I was insensibily hacking switch).

Thanks a lot for explanaion!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 14:01:31 2024 UTC