views:

102

answers:

6

How should the following boolean expression be written in PHP:

$foo = "";
if($var==TRUE){
    $foo = "bar";
}

or

if($var==TRUE){
    $foo = "bar";
}else{
    $foo = "";
}

or

$foo = ($var==TRUE) ? "bar": "";
+5  A: 

All of those work. It's preference. I'd consider initializing the variable first like you do in the 1st example. But for something this simple, the 3rd option is fine in my book.

Also, the 3rd doesn't have to be so verbose if $var is just a boolean value:

$foo = $var ? "bar" : "";
jtbandes
Unless they're specifically looking for true, then you'll want to use $foo = ($var === TRUE) ? "bar" : "";
Codeacula
+1  A: 

I like the first one:

$foo = "";
if($var==TRUE){
    $foo = "bar";
}

Since it is clear, concise, and easy to read.

Justin Ethier
A: 

Doesn't matter very much. I like the first one when there's a lot of elseif's so that you know the variable is always initialized. But it's really just a matter of preference.

Like the quotes, I like using single ones in php. No good reason :)

Michael Clerx
Oh and the third one could just as easily be $foo = $var ? 'bar' : '';
Michael Clerx
I tend to use a mix of quotes depending on what i'm outputting at the time, HTML strings tend to be generated using single quotes, while i believe double quotes are the only way to generate a new line feed? - "\r\n"
Stann0rz
+7  A: 

First off, true is not a constant, it's a token, so please don't uppercase it (I know some standards do that, but I think it confuses the meaning)...

Second, you don't need the redundant $var == true comparison inside the if. It's exactly the same as if ($var) { (For a double == comparison. An identical comparison === would need to be explicit).

Third, I prefer the pre-initialization. So:

$foo = '';
if ($var) {
    $foo = 'one status';
} else {
    $foo = 'another status';
}

If you don't need the else branch, just remove it. I prefer the pre-initialization since it forces you to initialize the variable, and it prevents cases where you forget to initialize it in one of the branches. Plus, it gives you a type hint when you go back to read the function later...

And for a simple branch like that, using the ternary syntax is fine. If there's more complex logic, I'd stay away though:

$foo = $var ? 'bar' : '';
ircmaxell
To be honest, the above codes gives no relation on what i'm currently working on and was something i knocked up in a few seconds in order to ask the question clearer, boolean values seemed more logical than string or integer comparison.Pre-initialization is something i've always done, but i've never been 'taught' PHP, or any language and just wondered if there was a best practice for this.Thanks for the advice ircmaxell.
Stann0rz
A: 

The right answer, as it often is the case is, "it depends". In this case,

if ($var==TRUE) $foo = "bar";
else $foo = "";

is very clear. But what is your context?

In general, the tertiary operator, your third option, should be used with extreme caution, as it very easily becomes hard to read.

But think in terms of what you want your code to MEAN, more than about what it DOES. Do you want to set your $foo to a "normal" value and then override it? Or do you want to set to something that depends on what $var is?

Something I find useful to change, that is not directly what you ask, but that is similar, is this, from

function func() {
    ...
    if ($condition) {
        do plenty
        of things
    }
    else {
        do plenty
        of things
    }
}

That, I generally like to change to:

function func() {
    ...
    if ($condition) {
        do plenty
        of things
        return;
    }
    do plenty
    of things
}

It generally makes sense.

Just ask yourself: "If someone who didn't know anything about my code read it, would it make sense to him? Or her?"

eje211
+1  A: 

I prefer the first one (except for the redundant test for the boolean) because it works consistently across languages, particularly those requiring to declare the variable (and maybe typify it) ahead of setting it.
Java:

String foo = "";
if (var) {
  foo = "Something";
}

JavaScript or JavaFX:

var foo = "";
if (var) {
  foo = "Something";
}

Etc.
One can use the 3rd form too but if the condition (or assignment) is complex, it is a bit less readable.

PhiLho