Why does this print false. Is it because month is string and true is boolean?
$month = "string";
if ($month === true) {
echo "true";
} else {
echo "false";
}
Why does this print false. Is it because month is string and true is boolean?
$month = "string";
if ($month === true) {
echo "true";
} else {
echo "false";
}
===
is comparison operator for value and type, so $month
have to be a boolean
(and also true
, of course). Is it?
You should use $month == true
(that will compare only value, regardless of type) or simply if($month)
(as don't exist months that could be 0)
When using '===' you are doing a comparison by value AND by type. Since $month = 'string';
is obviously of type string, it doesn't equal boolean true
, and so the expression evaluates to false.
To make it output "true", replace the "===" operator with "=="
Here's a link to a question here on SO which sums it up nicely
Yes.
== is used to compare, but using === (note the extra '=' sign) will also check the type of data. Because $month contains a string, and you are comparing it with a boolean, it will return false.
When you'd be using ==, it would return true.
at condition time if u have not declared the $month variable. then $month will be undefined and '===' true will returns false always.