php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16843 Switch fails when variable being switched equals zero
Submitted: 2002-04-26 02:42 UTC Modified: 2002-04-26 04:29 UTC
From: matrix at matrixscripts dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.2.0 OS: Red Hat Linux
Private report: No CVE-ID: None
 [2002-04-26 02:42 UTC] matrix at matrixscripts dot com
The following lines of code produce this:

"filled equals zero"

$filled = 0;
if($filled == 0){
     echo "filled equals zero";
}
switch($filled){
     case $filled == 0:
          echo "filled equals zero also"
     break;
}

As far as I can tell, there is no reason why that case isn't satisfied - $filled DOES equal 0, as the if statement proves, yet "filled equals zero also" never appears on the page. However, replacing "case $filled == 0:" with "case 0:" does work. This can only be a bug.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-26 03:18 UTC] yohgaki@php.net
The bug system is not the appropriate forum for asking support
questions. For a list of a range of more appropriate places to ask
for help using PHP, please visit http://www.php.net/support.php
 [2002-04-26 03:29 UTC] matrix at matrixscripts dot com
Perhaps you misunderstood my report - what I posted SHOULD echo "filled equals zerofilled equals zero also" but it doesn't.
Another example below:

$filled = 0;
switch($filled){
     case $filled == 0:
          echo "filled equals 0"
     break;
}
$filled = 100;
switch($filled){
     case $filled == 100:
          echo "filled equals 100"
     break;
}

When that is executed, it returns "filled equals 100". It SHOULD have returned "filled equals 0filled equals 100" but it doesn't - the only explanation is a bug in the switch code. This is not a bogus bug report.
 [2002-04-26 03:32 UTC] rasmus@php.net
What makes you think you can put expressions in a case statement?  You can't.  No bug here.
 [2002-04-26 03:34 UTC] derick@php.net
Your code is bogus, this is correct:

$filled = 0;

switch ($filled) {
    case 0: echo "filled equals zero";
        break;
}

Who told you you can use case $filled == 0: ? It's simply not ok.
 [2002-04-26 03:40 UTC] matrix at matrixscripts dot com
Alright, I understand what you are all saying. I'm sorry if my most recent comment sounded rude - I was annoyed that yohgaki@php.net thought I would be foolish enough to ask a support question here. I genuinely thought this was a bug. One question that's still at the back of my mind is, why does it work for everything else, except when it's being compared to 0?
 [2002-04-26 04:29 UTC] yohgaki@php.net
Should we better to consider change automatic message to be more polite? (or mention that it's a auto generate message?)

I'm not native English speaker, so I cannot tell if we need it ;)

Yasuo


 [2002-04-26 05:02 UTC] m dot ford at lmu dot ac dot uk
Well, I think ideally you need 3 versions:

1) When the poster genuinely thinks he's reporting a bug, but it's really a support issue, the response should be something like:

   This behaviour is not due to a bug in PHP, but is caused by
   some other issue.  For a range of places where you can seek 
   the appropriate help, please visit http://www.php.net/support.php

2) When a clear support question has been erroneously posted -- current message.

3) When it is not completely clear whether the post falls into either of the above, the response could read:

   From the information given, we believe this not to be a bug
   in PHP, but caused by some other issue.  For a range of
   places ... etc.

But if I had to go for just one, I'd choose number 3!

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 09:01:27 2024 UTC