DonutsAndCode
DonutsAndCode

Reputation: 33

printing boolean

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

Answers (3)

Hans Kerkhof
Hans Kerkhof

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

deceze
deceze

Reputation: 522005

$result is assigned true or false. That's important. It's not 1 or 0, it's true or false. When echoing true, it is output as 1. When echoing 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

zerkms
zerkms

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

Related Questions