php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20780 switch ... case ... statement cannot distinguish a string with integer 0
Submitted: 2002-12-02 23:54 UTC Modified: 2002-12-03 20:39 UTC
From: rosewell at cnk dot com dot cn Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.3 OS: Windows 2000
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: rosewell at cnk dot com dot cn
New email:
PHP Version: OS:

 

 [2002-12-02 23:54 UTC] rosewell at cnk dot com dot cn
<?php

$a = 'center';
switch ($a) {
    case 'left':
    case 0: echo 'left';break;
    case 'right':
    case 2: echo 'right';break;
    default:echo 'center';
}

?>

The script should generate 'center',yet generate 'left';what's more,any string assigned to variable $a will generate the same error. The reason is the PHP scripting engine cannot distinguish a string with integer 0. I wonder if the switch statement can only be applied on integers.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-03 01:16 UTC] chregu@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. 

Thank you for your interest in PHP.

See

http://ch.php.net/manual/en/language.types.string.php#language.types.string.conversion
 [2002-12-03 20:39 UTC] rosewell at cnk dot com dot cn
Thanks a lot for your tip. And now I've seen what a silly mistake I'd made. And I felt deeply sorry for disturbing you with such a silly fault.

But I wonder if PHP can introduce a Not_a_Number value just like the NaN in JavaScript. For PHP and JavaScript are both loose-type language, i.e. scripting language. For some convention, I think, it is unusual to automatically convert a string (especially a NOT_A_NUMBER string) to an integer, in which most case is converted to 0 and is not intuitionistic. And I find many people before have complained about it. So I'll be glad that you'll consider it.

Thank you again for your serious consideration.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 14:01:28 2024 UTC