[PYTHON] About type comparison by PHP

Overview

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.

Practice (PHP)

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.

Practice (Ruby)

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

Practice (Python)

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

Summary

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

About type comparison by PHP
About reference type
Sort by dict type value value
About batch_size specified by keras
About Perl, Python, PHP, Ruby
About Python string comparison operators
Stack processing speed comparison by language
Python Note: About comparison using is
About the basic type of Go
Think about transformation rock-paper-scissors by optimization
[Multivariate analysis] About quantification type I (001)
Think about menus by combinatorial optimization