Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么PHP认为0等于一个字符串?_Php_String_Numbers_Evaluate - Fatal编程技术网

为什么PHP认为0等于一个字符串?

为什么PHP认为0等于一个字符串?,php,string,numbers,evaluate,Php,String,Numbers,Evaluate,我有以下代码: $item['price'] = 0; /* Code to get item information goes in here */ if($item['price'] == 'e') { $item['price'] = -1; } 其目的是将项目价格初始化为0,然后获取相关信息。如果价格被告知为“e”,则表示交换而不是卖出,卖出作为负数存储在数据库中 也有可能将价格保留为0,因为该项目是奖金,或者因为价格将在稍后设置 但是,每当没有设置价格时(初始值为0),上面指

我有以下代码:

$item['price'] = 0;
/* Code to get item information goes in here */
if($item['price'] == 'e') {
    $item['price'] = -1;
}
其目的是将项目价格初始化为0,然后获取相关信息。如果价格被告知为“e”,则表示交换而不是卖出,卖出作为负数存储在数据库中

也有可能将价格保留为0,因为该项目是奖金,或者因为价格将在稍后设置

但是,每当没有设置价格时(初始值为0),上面指示的
if
循环的计算结果为true,价格设置为-1。也就是说,它认为0等于“e”

这怎么解释呢


当价格提供为0(初始化后)时,行为不稳定:有时if计算为true,有时计算为false。*

您应该使用
===
而不是
==
,因为普通运算符不比较类型。相反,它将尝试对项目进行类型转换

同时,
==
考虑项目的类型

  • =
    表示“等于”
  • =
    的意思是“eeeeh..有点像”

即使值的类型不同,==运算符也会尝试匹配值。例如:

'0' == 0 will be true
如果还需要类型比较,请使用===运算符:

'0' === 0 will be false

您正在执行的是为您整理类型的
==

0
是一个int,因此在本例中,它将把
'e'
转换为一个int。该int不可分解为一,将变为
0
。字符串
'0e'
将变为
0
并匹配

使用
==

发件人:

使用==和其他非严格表达式比较字符串和数字 比较运算符当前的工作方式是将字符串强制转换为数字, 然后对整数或浮点数执行比较。这 结果在许多令人惊讶的比较结果中,最显著的是 也就是说,0==“foobar”返回true

但是,此行为在以下方面发生了更改:

当与数字字符串进行比较时,PHP8使用数字比较。 否则,它会将数字转换为字符串并使用字符串 比较

PHP7

PHP8在进行比较之前将数字转换为字符串

0 == 'foobar' // false
0 == '' // false
4 == '4e' // false ('4e' is considered non-numeric therefore 4 is cast as a string and becomes '4')

这是一个重大变化,因此它是在一个新的主要PHP版本中实现的。此更改破坏了依赖于旧行为的脚本中的向后兼容性。

这是由于PHP如何执行比较操作,表示:

如果将数字与字符串进行比较,或者比较涉及数字字符串,则会将每个字符串转换为数字,并以数字形式执行比较。[…]当比较为
==
时,不会发生类型转换==因为这涉及比较类型和值

由于第一个操作数是一个数字(
0
),第二个操作数是一个字符串(
'e'
),因此该字符串也会转换为一个数字(另请参见)。字符串数据类型的手动页面定义了如何执行:

在数值上下文中计算字符串时,结果值和类型的确定如下所示

如果字符串不包含任何字符“
”、“
e
”或“
e
”,并且数值符合整数类型限制(由
PHP\u INT\u MAX
定义),则字符串将作为整数计算。在所有其他情况下,它将作为浮动进行评估

在这种情况下,字符串是
'e'
,因此它将被计算为浮点:

该值由字符串的初始部分给出。如果字符串以有效的数字数据开头,则将使用该值。否则,该值将为
0
(零)。有效数字数据是可选符号,后跟一个或多个数字(可选包含小数点),后跟可选指数。指数是一个“
e
”或“
e
”后跟一个或多个数字


由于
'e'
不是以有效的数字数据开始的,它的计算结果是float
0
在PHP中有一个相当方便的方法来验证“0”、“false”、“off”As==false和“1”、“on”、“true”As==true的组合,这常常被忽略。它对于解析GET/POST参数特别有用:

filter_var( $item['price'], FILTER_VALIDATE_BOOLEAN );
它与这个用例并没有完全的相关性,但考虑到相似性和事实,这是搜索结果在询问验证(字符串)“0”为假的问题时倾向于发现的,我认为这会帮助其他人


您的问题是双等运算符,它将右成员的类型转换为左成员的类型。如果您愿意,请使用strict

if($item['price'] == 'e') {
    $item['price'] = -1;
}
让我们回到您的代码(复制在上面)。在这种情况下,在大多数情况下,$item['price']是一个整数(显然,当它等于e时除外)。同样地,根据PHP的规则,PHP将
“e”
类型转换为整数,从而生成
int(0)
。(不相信我?

要轻松摆脱这种情况,请使用triple equal(精确比较)操作符,它将检查类型,而不会隐式地进行类型转换

附言:一个PHP有趣的事实:
a==b
并不意味着
b==a
。以您的示例为例,将其颠倒过来:
如果('e“==$item['price'])
永远不会实际实现,只要$item['price']始终是整数

"ABC" == 0
计算
true
,因为首先
将“ABC”
转换为整数并变为
0
,然后将其与
0
进行比较

这是PHP语言的一种奇怪的行为:通常人们希望
0
被提升为字符串
“0”
,然后与
“ABC”
进行比较,结果是
false
。 在其他语言(如JavaScript)中可能会出现这种情况,其中弱比较
“ABC”==0
evalua
"ABC" == 0
"ABC" === 0
"123" === 123
(string)"123" === (string)123
(string)"123" === (string)0
$item['price'] = 0;
/*code to get item information goes in here*/
if((string)$item['price'] == 'e') {
    $item['price'] = -1;
}
// Normal comparison using the == Operator
echo (0 == "0"); // true
echo (0 == "a"); // true
echo (0 == "safta!"); // true
echo (1000 == "bla"); // false. It appears that PHP has a weird behavior only with the number / string 0 / "0" according to the past 3 examples.
echo (23 == "23"); // true. So as we said, PHP has a problem (not a problem but weird behavior) only when the number / string 0 (or "0") is present
echo (23 == "24"); // false. values aren't equal (unlike last example). The type is less relevant with the == operator as we can see.

// Now using the === and !== Operators
echo ("0" === 0); // false, since === requires both value and type to be the same. Here, type is different (int vs string)
echo ("0" !== 0); // true because they aren't the same in terms of === comparison (type is different and that's why it's true)
echo ("bla" === "blaa"); // false because the values are not the same. The type is the same, but === checks for both equal type and equal value.

//Now using casting and === Operator:
echo ((string)123 === "123"); // true. The casting of the int 123 to string changed it to "123" and now both variables have same value and are of same type
echo ((int)"123" === 123); // true. The casting of the string 123 to int, changed it to int, and now both variables are of same value and type (which is exactly what the === operator is looking for)

// Now using casting and == Operator. Basically, as we've seen above, the == care less for the
// type of var, but more to the value. So the casting is less relevant here, because even
// without casting, like we saw earlier, we can still compare string to int with the == operator
// and if their value is same, we'll get true. Either way, we will show that:
echo ((string)123 == "123"); // true. The casting of the int 123 to string changed it to "123" and now both vars have same value and are of same type
echo ((int)"123" == 123); // true. The casting of the string 123 to int, changed it to int, and now both vars are of same value and type (which is exactly what the === operator is looking for)