比较开关内数字的奇怪问题
Strange issue comparing numbers inside switch
我正在尝试使用 switch case
从类别 ID 生成 css class 名称。
我在 switch case 中有多个条件,但我们只会将这个视为它创建奇怪的输出。
示例代码:
<?php
$value = '907';//base value
$value_array = str_split($value);//create array of string, if its int.
var_dump($value_array);//debug whats in array
switch($value_array[0]){
case 9:
$final = 'i came inside 9';
if($value_array[1].$value_array[2] == 07){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == 09){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
break;
}
echo $final;
以上代码给出的输出为 [$value is 907]
:
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "0"
[2]=>
string(1) "7"
}
i came inside 907
这是正确的行为。但是,如果我将基值从 907 更改为 909,则输出为 [$value is 909]
.
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "0"
[2]=>
string(1) "9"
}
i came inside 9
输出应该是i came inside 909
。
这是为什么?
为什么它适用于 907
而不适用于 909
,即使它们具有相同的数据类型?
我知道它们是字符串,我应该将字符串与字符串进行比较,但为什么它适用于一个示例而不适用于另一个示例?
07
和 09
是 octal numbers,其中 09
是一个无效的八进制数,所以它最终会是 0。这就是为什么你的代码没有随心所欲地工作。
要解决它,只需将其放在引号中,例如
if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
当您使用 07
、PHP interprets it as an octal number 时。它知道09
不是八进制,因为9
在八进制系统中是无效的。
尝试 7
和 9
,或 '07'
和 '09'
。
<?php
$value = '907'; //base value
$value_array = str_split($value); //create array of string, if its int.
var_dump($value_array); //debug whats in array
switch ($value_array[0])
{
case 9:
$final = 'i came inside 9';
if ($value_array[1].$value_array[2] == '07')
{
//check whther last 2 digits are 07
$final = 'i came inside 907';
}
elseif($value_array[1].$value_array[2] == '09')
{
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
break;
}
echo $final;
您正在将数组值与格式化为八进制数的整数进行比较(参见 http://php.net/manual/de/language.types.integer.php)。
07
是一个有效的八进制数,代表值 7
并且您的比较有效。
另一方面,09
是无效的八进制数。因此比较无效。
为了解决您的问题,您需要将 ' 放在值周围,以便将它们解释为字符串。
if($value_array[1].$value_array[2] == '07'){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == '09'){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
因为在 php 09
中会将其视为八进制数并将其转换为 0
而对于 07
它总是 07
当你尝试 echo 09
它会输出你 0
而对于 07
它会输出 07
所以不要松散地比较 ==
你需要使用严格的比较 ===
即
if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
我正在尝试使用 switch case
从类别 ID 生成 css class 名称。
我在 switch case 中有多个条件,但我们只会将这个视为它创建奇怪的输出。
示例代码:
<?php
$value = '907';//base value
$value_array = str_split($value);//create array of string, if its int.
var_dump($value_array);//debug whats in array
switch($value_array[0]){
case 9:
$final = 'i came inside 9';
if($value_array[1].$value_array[2] == 07){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == 09){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
break;
}
echo $final;
以上代码给出的输出为 [$value is 907]
:
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "0"
[2]=>
string(1) "7"
}
i came inside 907
这是正确的行为。但是,如果我将基值从 907 更改为 909,则输出为 [$value is 909]
.
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "0"
[2]=>
string(1) "9"
}
i came inside 9
输出应该是i came inside 909
。
这是为什么?
为什么它适用于
907
而不适用于909
,即使它们具有相同的数据类型?我知道它们是字符串,我应该将字符串与字符串进行比较,但为什么它适用于一个示例而不适用于另一个示例?
07
和 09
是 octal numbers,其中 09
是一个无效的八进制数,所以它最终会是 0。这就是为什么你的代码没有随心所欲地工作。
要解决它,只需将其放在引号中,例如
if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
当您使用 07
、PHP interprets it as an octal number 时。它知道09
不是八进制,因为9
在八进制系统中是无效的。
尝试 7
和 9
,或 '07'
和 '09'
。
<?php
$value = '907'; //base value
$value_array = str_split($value); //create array of string, if its int.
var_dump($value_array); //debug whats in array
switch ($value_array[0])
{
case 9:
$final = 'i came inside 9';
if ($value_array[1].$value_array[2] == '07')
{
//check whther last 2 digits are 07
$final = 'i came inside 907';
}
elseif($value_array[1].$value_array[2] == '09')
{
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
break;
}
echo $final;
您正在将数组值与格式化为八进制数的整数进行比较(参见 http://php.net/manual/de/language.types.integer.php)。
07
是一个有效的八进制数,代表值 7
并且您的比较有效。
09
是无效的八进制数。因此比较无效。
为了解决您的问题,您需要将 ' 放在值周围,以便将它们解释为字符串。
if($value_array[1].$value_array[2] == '07'){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == '09'){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}
因为在 php 09
中会将其视为八进制数并将其转换为 0
而对于 07
它总是 07
当你尝试 echo 09
它会输出你 0
而对于 07
它会输出 07
所以不要松散地比较 ==
你需要使用严格的比较 ===
即
if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
$final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
$final = 'i came inside 909';
}