A practical example for better understanding:
<?php
$variavel = 'true';
if($variavel == "true"){
echo "1";
}
if($variavel == true){
echo "2";
}
if($variavel === true){
echo "3";
}
if($variavel = true){
echo "4";
}
echo $variavel;
The values that will be shown on the screen: 1, 2, 4 and 1
($variable == "true") = True because it’s the same string.
($variable == true) = True because true equals true.
($variable === true) = False because true is true, but types are different, one is a string and the other is boolean.
($variable = true) = true since it is a simple value assignment, this case will only be false if the assignment fails, usually the false return happens when the value for comparison comes from a function, the function can return something that is impossible to assign.
And the value 1 of the end is the result of the "$variable" since after the ($variable = true) its value passed to true because of the assignment and when showing on the screen it shows 1 that is true for php, if you do so 'echo true;' the result on the screen will be 1 too.
That’s because you did an assignment within the
if
, using the operator=
. The correct would be two equal signs to compare,==
.– Woss