How should boolean expressions be written in PHP?

我是研究僧i 提交于 2019-12-23 08:58:51

问题


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

回答1:


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



回答2:


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



回答3:


I like the first one:

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

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




回答4:


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.




回答5:


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 :)




回答6:


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



来源:https://stackoverflow.com/questions/3514091/how-should-boolean-expressions-be-written-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!