php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77845 Error in the comparaison
Submitted: 2019-04-04 11:10 UTC Modified: 2019-04-04 11:21 UTC
From: baklouti dot med at gmail dot com Assigned:
Status: Not a bug Package: *Compile Issues
PHP Version: 7.2.16 OS: ubuntu 18.04
Private report: No CVE-ID: None
 [2019-04-04 11:10 UTC] baklouti dot med at gmail dot com
Description:
------------
I am testing the value of $id to do some processing. I retrieve the variable $id from an array ($test) and converted to integer, for the case that $id = 0 when using "switch" it can't be detected and if I used the "if" it is working fine.
I tried with the "==" and the "===" and also not working with "switch".
I am not using the two cases in the same time, i tested the "if" and "switch"
separately.

  $id = (integer)$test['id'];

  if ($id === 0 ) {return 1;}
        
  switch ($id) {
     case ($id === 0):
        return 2;
      ....



Expected result:
----------------
The expected result is the bloc inside the case ($id ===0) is processed. 

Actual result:
--------------
The actual result that it is passed for another case.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-04 11:21 UTC] kalle@php.net
-Status: Open +Status: Not a bug
 [2019-04-04 11:21 UTC] kalle@php.net
Because `($id === 0)` is evaluated as an expression before, meaning that essentially the `case` statement now looks like `case 1:`. The correct way to do that which you are trying to would be to make your case statement simply look like `case 0:`.

Please refer to the documentation int the future:
https://www.php.net/manual/en/control-structures.switch.php
 [2019-04-04 14:34 UTC] baklouti dot med at gmail dot com
Yes, I try it with "case 0:" , and the same issue is happening.
 [2019-04-04 14:37 UTC] spam2 at rhsoft dot net
hell post the code you are actually using instead f**g decriptions ike " am not using the two cases in the same time" - switch works, full stop, echo what $id contains really at that moment
 [2019-04-04 14:50 UTC] baklouti dot med at gmail dot com
ok switch works so fine, you can test it if you are so sure and tell us the result, because when id in the array test contains 0, the $is =0, and even test it with var_dump, and gettype to be sure before post this, thank you.
 [2019-04-11 21:21 UTC] a at b dot c dot de
I didn't understand that last part.

More useful for use would be a complete example that we could run that demonstrates your issue. The bug reporting form offers space for you to enter such a "test script". The code you have provided does not demonstrate your problem because if $id===0 then the second line of your code will cause "return 1;" to be evaluated and the switch will never even be reached.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC