Reputation: 33
Thank you for dropping by.
Code
$variable = 10;
$variableOne = 10;
$result = $variable == $variableOne;
echo $result;
When comparison between $variable and $variableOne returns false, Why is $result not assigned 0? However the end result is otherwise and $result is assigned 1 when comparison returns true.
Thank you for your time, PHP Beginner.
Upvotes: 3
Views: 161
Reputation: 442
$result is not the result of an expression but in your code is a (invalid) assignment.
Syntax should be like this:
if($variable == $variableOne){
$result = true;
} else {
$result = false;
}
echo $result;
Upvotes: -1
Reputation: 522005
$result
is assigned true
or false
. That's important. It's not 1
or 0
, it's true
or false
. When echo
ing true
, it is output as 1
. When echo
ing false
, it is output as an empty string. Try var_dump($result)
instead to see the difference.
See http://www.php.net/manual/en/language.types.string.php#language.types.string.casting
Upvotes: 2
Reputation: 254886
Perform var_dump($result);
and see that this variable is boolean, because it has been assigned with ==
operator evaluation which is always boolean.
Upvotes: 0