-1

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";
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278

4 Answers4

2

=== 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)

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
1

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

Community
  • 1
  • 1
NorthBridge
  • 639
  • 8
  • 20
1

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.

Niek van der Steen
  • 1,413
  • 11
  • 33
0

at condition time if u have not declared the $month variable. then $month will be undefined and '===' true will returns false always.

user2706194
  • 199
  • 8
  • Any test against $month will return false except `is_null()` which will give an `E_NOTICE` and return true, and `empty()` will return true – David Wilkins Feb 28 '14 at 13:48
  • Ignore the "E_NOTICE" part of my comment above, that test will trigger an `E_NOTICE` if run on an undeclared variable. – David Wilkins Feb 28 '14 at 13:55