Tom
Tom

Reputation: 30698

Boolean evaluation in reverse

Why is the boolean evaluation done in reverse in the following PHP code, as opposed to putting "false" at the end?

while (false !== ($obj = readdir($dh))) {
    // do something
}

(from one of the user examples in http://php.net/manual/en/function.unlink.php)

I've seen this way of writing evaluations elsewhere but never really understood why it's done. I've never studied computer science so this might be a real 101 question.

Upvotes: 2

Views: 114

Answers (1)

Marko D
Marko D

Reputation: 7576

Personal preference so you don't by mistake assign value instead of doing a comparison.

For example

// doesn't generate an error, hard to track
if($value = false)
...

but

// fatal error, you know that you did = instead of ==
if(false = $value)

Upvotes: 7

Related Questions