Home  >  Article  >  Backend Development  >  Can PHP achieve congruence in switch?

Can PHP achieve congruence in switch?

WBOY
WBOYOriginal
2016-12-01 01:28:011261browse

<code>        switch($value){
            case null: echo 'null';
                break;
            case '': echo '空';
                break;
        }
            
</code>

The problem now is that when $value = '', switch will enter the first case.
Can we achieve congruent judgment and let the switch enter the second case?

Reply content:

<code>        switch($value){
            case null: echo 'null';
                break;
            case '': echo '空';
                break;
        }
            
</code>

The problem now is that when $value = '', switch will enter the first case.
Can we achieve congruent judgment and let the switch enter the second case?

You can change your mind and look at the code

<code>switch(true) {
    case null === $value : echo 'null';
        break;
    case '' === $value: echo '空';
        break;
}</code>

No, switch does a loose comparison.

What you said above is correct. Change your thinking

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn