php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37825 swtich case structure faulty string usage
Submitted: 2006-06-16 10:27 UTC Modified: 2006-06-16 13:54 UTC
From: michael dot nm at mail dot dk Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.1.4 OS: windows web server 2003
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: michael dot nm at mail dot dk
New email:
PHP Version: OS:

 

 [2006-06-16 10:27 UTC] michael dot nm at mail dot dk
Description:
------------
When using a variable og type String, the switch structure behaives odd.

Reproduce code:
---------------
<?php
$modes = array("byId", "byName", "asSelect", "", null, "as", "Id", "Name", "Select");

print "<pre>";
print_r($modes);
print "</pre>";

foreach($modes as $mode) {
	print $mode;
	switch($mode) {
		case "case1":
			print "<div>case1 => uses ".$mode."</div>";
			break;
		case "case2" || "case3":
			print "<div>case2 or case3 => uses ".$mode."</div>";
			break;
		default:
			print "<div>default => cannot use ".$mode."</div>";
	}
}
?>

Expected result:
----------------
Array
(
    [0] => byId
    [1] => byName
    [2] => asSelect
    [3] => 
    [4] => 
    [5] => as
    [6] => Id
    [7] => Name
    [8] => Select
)

byId
default => cannot use
byName
default => cannot use
asSelect
default => cannot use
("" and null)
default => cannot use
default => cannot use
as
default => cannot use
Id
default => cannot use
Name
default => cannot use
Select
default => cannot use

Actual result:
--------------
Array
(
    [0] => byId
    [1] => byName
    [2] => asSelect
    [3] => 
    [4] => 
    [5] => as
    [6] => Id
    [7] => Name
    [8] => Select
)

byId
case2 or case3 => uses byId
byName
case2 or case3 => uses byName
asSelect
case2 or case3 => uses asSelect
default => cannot use
default => cannot use
as
case2 or case3 => uses as
Id
case2 or case3 => uses Id
Name
case2 or case3 => uses Name
Select
case2 or case3 => uses Select

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-16 13:54 UTC] mike@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

There\'s no or-operator in a case statement.
Your expression actually evaluates to TRUE.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 20:01:36 2025 UTC