According to the following description, PHP seems to be TRUE even if the type to be compared is different. https://www.php.net/manual/ja/migration80.incompatible.php Since 0 == "0" is TRUE, it seems that it is necessary to set 0 === "0" to verify the type match.
I wrote a simple process in PHP. It seems that 0 == "0" is really true.
<?php
if (0 == "0") {
echo "true";
} else
echo "false";
?>
Execution result
% php test.php
true
Next, try running it with the following code. The only change is that "if (0 ==" 0 ")" has changed to "if (0 ===" 0 ")".
<?php
//Also verify the type to be compared
if (0 === "0") {
echo "true";
} else
echo "false";
?>
Execution result
% php test.php
false
I was able to confirm that it would be false if I even verified the type.
In Ruby, "0 ==" 0 "" is false because numbers and strings cannot be compared. This is as expected.
if 0 == "0" then
puts "true"
else
puts "false"
end
Execution result
% ruby test.rb
false
Even in Python, "0 ==" 0 "" is false because the type is different. This is also as expected.
if 0 == "0":
print("true")
else:
print("false")
% python test.py
false
PHP seems to have a different design concept from Ruby and Python. It seems that both the numerical value 0 and the character string 0 will be true because PHP only needs to match the contents of the comparison target.
Recommended Posts