1

I often see scripts testing if a variable is different from FALSE.

This is an example from php man of "fgetcsv" function, but I think I saw that on Java too.

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    //...
}

Even if it doesn't change much, it would seem more natural to go like :

while (($data = fgetcsv($handle, 1000, ",")) === TRUE) {
    //...
}

Is there a reason the second code is less logic or less efficient ?

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • 1
    [Null vs. False vs. 0 in PHP](http://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php) and [How does true/false work in PHP?](http://stackoverflow.com/questions/2382490/how-does-true-false-work-in-php) – Richard Chambers Sep 12 '15 at 13:35

1 Answers1

2

A common idiom of PHP is to have a function return some meaningful value, or a boolean FALSE in case of some failure. In fgetcsv's case, an index array is returned or FALSE when there are no more values to return.

Using === TRUE simply won't work here - an indexed array isn't a boolean TRUE, but it is most definitively !== FALSE.

Mureinik
  • 297,002
  • 52
  • 306
  • 350