我有以下代码:
$item['price'] = 0;
/*code to get item information goes in here*/
if($item['price'] == 'e') {
$item['price'] = -1;
}
它旨在将项目价格初始化为0,然后获取有关它的信息。如果价格被告知为'e',则表示交易而不是卖出,卖出作为负数存储在数据库中。
还有可能将价格保留为0,因为该项目是奖金或因为价格将在稍后设定。
但是,只要没有设置价格,使其初始值为0,上面指出的if
循环评估为真,价格设置为-1。也就是说,它认为0等于'e'。
怎么解释这个?
编辑:当价格提供为0(初始化后)时,行为不稳定:有时if评估为true,有时评估为false。
你正在做==
为你排序的类型。
0
是一个int,所以在这种情况下它将把'e'
强制转换为int。这不是一个可解析的,并将成为0
。一个字符串'0e'
将成为0
,并将匹配!
使用===
这是由于PHP如何进行==
comparison operator表示的比较操作:
如果将数字与字符串进行比较或比较涉及数字字符串,则每个字符串将转换为数字,并且数字执行比较。 [...]当比较为
===
或!==
时,不会发生类型转换,因为这涉及比较类型和值。
由于第一个操作数是一个数字(0
),第二个操作数是一个字符串('e'
),因此该字符串也会转换为数字(另请参见table Comparison with Various Types)。字符串数据类型的手册页定义了string to number conversion的完成方式:
在数值上下文中计算字符串时,结果值和类型将按如下方式确定。
如果字符串不包含任何字符'
.
','e
'或'E
'且数值适合整数类型限制(由PHP_INT_MAX
定义),则字符串将被计算为整数。在所有其他情况下,它将被评估为浮点数。
在这种情况下,字符串是'e'
,因此它将被评估为float:
该值由字符串的初始部分给出。如果字符串以有效数字数据开头,则这将是使用的值。否则,该值将为
0
(零)。有效数字数据是可选符号,后跟一个或多个数字(可选地包含小数点),后跟可选指数。指数是'e
'或'E
',后跟一个或多个数字。
由于'e'
不是以有效的数字数据开头,因此评估为浮动0
。
"ABC" == 0
评估true
,因为第一个"ABC"
被转换为整数并变成0
然后它与0
进行比较。
这是PHP语言的奇怪行为:通常人们会期望0
被提升为字符串"0"
,然后将"ABC"
与false
进行比较。也许这就是其他语言中发生的事情,例如JavaScript,弱比较"ABC" == 0
评估false
。
进行严格的比较可以解决问题:
"ABC" === 0
评估false
。
但是如果我需要将数字作为字符串与数字进行比较呢?
"123" === 123
评估false
,因为左右项是不同类型的。
实际需要的是一个弱的比较,没有PHP类型杂耍的陷阱。
解决方案是明确地将术语提升为字符串,然后进行比较(严格或弱无关紧要)。
(string)"123" === (string)123
是
true
而
(string)"123" === (string)0
是
false
应用于原始代码:
$item['price'] = 0;
/*code to get item information goes in here*/
if((string)$item['price'] == 'e') {
$item['price'] = -1;
}
==运算符将尝试匹配值,即使它们属于不同类型。例如:
'0' == 0 will be true
如果您还需要类型比较,请使用===运算符:
'0' === 0 will be false
你的问题是双等号运算符,它将正确的成员类型化为左边的类型。如果您愿意,请使用strict。
if($item['price'] == 'e') {
$item['price'] = -1;
}
让我们回到你的代码(上面复制)。在这种情况下,在大多数情况下,$ item ['price']是一个整数(显然,除非它等于e)。因此,根据PHP的定律,PHP会将"e"
转换为整数,从而产生int(0)
。 (不相信我?<?php $i="e"; echo (int)$i; ?>
)。
为了轻松摆脱这种情况,请使用三重相等(精确比较)运算符,它将检查类型并且不会隐式地进行类型转换。
P.S:一个PHP有趣的事实:a == b
并不意味着b == a
。以你的例子反转它:如果$ item ['price']总是一个整数,if ("e" == $item['price'])
将永远不会实现。
在PHP中有一个非常方便的方法,用于验证混合的“0”,“假”,“关闭”为== false和“1”,“on”,“true”为== true,这经常被忽略。它对解析GET / POST参数特别有用:
filter_var( $item['price'], FILTER_VALIDATE_BOOLEAN );
它与这个用例没有多大的关系,但鉴于相似性和事实,这是搜索结果,当询问验证(字符串)“0”为错误的问题时,我认为它会帮助其他人。
您应该使用===
而不是==
,因为普通运算符不会比较类型。相反,它会尝试对项目进行类型转换。
与此同时,===
考虑了物品的类型。
===
的意思是“等于”,==
的意思是“eeeeh ..有点看起来像”我认为最好通过我做的例子来展示,同时遇到同样奇怪的行为。查看我的测试用例,希望它能帮助您更好地理解行为:
//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). 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 its true)
echo ("bla" === "blaa"); //false because the values are not the same. The type is the same but === check 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 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)
//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)
希望能帮助到你。