I saw this question in another group
Where did the ssid go?
I saw this question in another group
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>
In the results, we can find that echo $filedStr;
This step has no output.
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>
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.
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>