Home » Php » How should boolean expressions be written in PHP?

How should boolean expressions be written in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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": "";
How to&Answers:

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' : '';

Answer:

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" : "";

Answer:

I like the first one:

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

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

Answer:

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.

Answer:

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 🙂

Answer:

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?”