Isn't the switch written like this? Where did the ssid go?

WBOY
Release: 2016-08-18 09:15:47
Original
2740 people have browsed it

I saw this question in another group
Where did the ssid go?
Isn't the switch written like this? Where did the ssid go?

Isn't the switch written like this? Where did the ssid go?

Reply content:

I saw this question in another group
Where did the ssid go?
Isn't the switch written like this? Where did the ssid go?

Isn't the switch written like this? Where did the ssid go?

We can echo output variables in some places, as shown in the following code.

<code><?php
$filed = array(0, 707, 708, 'ssid');
$dd = array();

foreach ($filed as $value) {
    echo $value . "\n";
    switch ($value) {
        case 0:
            $filedStr = 'sindex';
        break;
        case 707:
            $filedStr = 'login';
        break;
        case 708:
            $filedStr = 'register';
        break;
        case 'ssid':
            $filedStr = 'ssid';
            echo $filedStr;
        break;
        
        default:
        break;
    }
}</code>
Copy after login

In the results, we can find that echo $filedStr;This step has no output.

Isn't the switch written like this? Where did the ssid go?

At this time, you should read the official PHP documentation to find out why this happens. The reason is switch.

In the PHP official documentation, what, this is in English, of course there is also Chinese. There is a sentence in it:

Attention if you have mixed types of value in one switch statemet it can make you some trouble

Of course, there is a plan below:

<code><?php

$string="2string";

switch($string)
{
    case (string) 1:
        echo "this is 1";
        break;
    case (string) 2:
        echo "this is 2";
        break;
    case '2string':
        echo "this is a string";
        break;
}

?></code>
Copy after login

It is mentioned that PHP uses dynamic type conversion, which is what this blog talks about.

The variable type of dynamic language changes with the stored variable, that is, the variable type changes according to the specific environment.

When $value is 'ssid', when case 0 is executed, it must be converted to an integer and compared with it. Because they are equal after conversion, the following 'ssid' cannot be matched.

Because in your switch matching, case is an integer, which will cause the string to be forced to integer, ssid to int to 0 during matching, which will not match to case 'ssid'

The ssid is definitely not matched

Thank you everyone for your answers, I understand.

<code>// 只取出以合法数字开始整型和浮点型,取到第一个非法数字截止...
intval('1234ssid');// 1234
intval('ssid');// 0
var_dump('ssid' == 0);//so, 这里就为`true`,就会执行它所对应的代码段.ssid想再去匹配已经没有机会了哈
</code>
Copy after login
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!