I was reading PHP's Array man and I saw this:
<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
In the readdir man page, it says "Correctly way of looping".
I'd like to know if there's a difference between X !== false
and false !== X
. Thanks!
No, there's no difference. The reason you'll sometimes see false == x
instead of x == false
is that it helps prevent accidentally typing x = false
which is permissible in an if or loop structure but is probably not what you want. false = x
is nonsensical, and will generate an error instead of silently assigning something.
No it's the same
writing this
false !== X
is also known as "Yoda condition" :) Very useful when you want to avoid unexpected assignments (using = instead of ==)