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

Can PHP achieve congruence in switch?

WBOY
Release: 2023-03-02 14:54:01
Original
1286 people have browsed it

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

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>
Copy after login
Copy after login

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>
Copy after login

No, switch does a loose comparison.

What you said above is correct. Change your thinking

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template