"ABC" == 0
evaluates to true because first "ABC" converted to an integer and becomes 0 then compared to 0 .
This is a strange behavior of the PHP language: you can usually expect 0 be cast to the string "0" and then compared to "ABC" with the result false . Perhaps what happens in other languages, such as JavaScript, where a weak comparison "ABC" == 0 evaluates to false .
A rigorous comparison solves the problem:
"ABC" === 0
evaluates false
But what if I need to compare numbers as strings with numbers?
"123" === 123
evaluates to false because the left and right members are of different types.
What is really needed is a weak comparison without traps in PHP juggling.
The solution is to explicitly push the terms into the string and then compare (strong or weak no longer matters).
(string)"123" === (string)123
is an
true
while
(string)"123" === (string)0
is an
false
Applied to the source code:
$item['price'] = 0; /*code to get item information goes in here*/ if((string)$item['price'] == 'e') { $item['price'] = -1; }
Paolo Feb 21 '18 at 17:59 2018-02-21 17:59
source share