PHP, better to set the variable before if or use if/else?

Posted by DssTrainer on Stack Overflow See other posts from Stack Overflow or by DssTrainer
Published on 2011-03-17T16:02:00Z Indexed on 2011/03/17 16:10 UTC
Read the original article Hit count: 146

Filed under:
|
|

So a simple one that I just never could find a straight answer on.

What is better (performance or otherwise):

$var = false;
If ($a == $b) {
    $var = true;
}

or

If ($a == $b) {
    $var = true;
} else {
    $var = false;
}

I've heard arguments for both ways. I find the first cleaner to ensure I have it set, and a little less code too. The pro being that you may only need to set it once without conditional. But the con being that if the argument is true, it gets set twice.

I am assuming the second way is probably best practice

© Stack Overflow or respective owner

Related posts about php

Related posts about variables