This if else statement does not work: (in this situation it displays ‘x is euqal to null’)
<?php
$x = 0;
if ($x == 'null'){
echo 'x is euqal to null';}
else if ($x == 0){
echo 'x is equal to zero!';}
else if ($x == 1){
echo 'x is equal to one!';}
?>
My reason for needing this is that I am writing a quiz program, and one of the features is that it displays a tick, cross or three dots next to each question depending on whether the user has done it and what their score is.
If the score for that question = ‘null’ then they haven’t yet done it ( display …)
If the score for that question = 1 then they have done it, and got it right (display tick)
If the score for that question = 0 then they’ve none it, but got it wrong (display cross)
My actual code is:
$score = ($_SESSION['score']);
if ($score[$questionList[$loop]['ID']]=='null'){ ?>
<p style="display:inline;"> ... </p> <?php }
else if ($score[$questionList[$loop]['ID']]==1){ ?>
<p style="display:inline;"> ✓ </p> <?php }
else if ($score[$questionList[$loop]['ID']]==0){ ?>
<p style="display:inline;"> X </p> <?php } ?>
Although it’s part of a much bigger page.
I am quite new to PHP, so am probably being incredibly thick here, but any suggestions will be much appreciated, thanks : )
I believe the first condition returns true because strings are equal to 0. See documentation on comparison operators.
The solution is to replace your
==with===. Ex: