0
Information about a variable in PHP
$x = 1; var_dump($x) // int(1) $y = true; var_dump($y) //bool(true) var_dump($x == $y) //bool(true) Why in ($x == $y) case we get: bool(true)?
3 ответов
+ 4
Hi, because true == 1, it doesn't matter that one is int and other boolean.
Now if you try to use identical (===) you will get false, since in addition to comparing the value, you are comparing data type.
+ 4
You're welcome!
Actually only 1 equals true and 0 equals false; they are equal in value but not in type, since 1 and 0 are integers, while true and false are boolean type
You can try for example with:
$ x = "1";
$ y = 1;
Same value (both 1) but different type (string and int)
If you try "1" == 1 you will get true, but if you try "1" === 1 you will get false, since they are different types of data.
On the other hand if you try for example 1 == "2", 1 == 2, 1 === "2" or 1 === 2 you will get false in all cases, since they are not equal in value (1 ≠ 2)
Rember that value is one thing and type is another, some examples:
$a = 0; (value = 0(false), type = int)
$x = 1; (value = 1(true), type = int)
$b = 2; (value = 2, type = int)
$y = 1.5; (value = 1.5, type = float)
$z = true ; (value = true(1), type = bool)
$g = false; (value = false(0), type = bool)
$h = "hello"; (value = hello, type = string)
0
Thank you for your answer!
As I understand any data type (for example: 1, -5.5 or “string”) == true, and only 0 == false?