How to simplify the following IF in PHP?

16

I had to do a check to concatenate content to a variable, but I think the service got a little "hog" and wanted to see if anyone could help me simplify this check.

if($IdUserOnline2){
    $IdsInteracoes .= implode(',',$IdUserOnline2);
    if($IdUserComum2){
        $IdsInteracoes .= ','.implode(',',$IdUserComum2);
        if($IdUserNovo2){
            $IdsInteracoes .= ','.implode(',',$IdUserNovo2);
        }
    }elseif($IdUserNovo2){
        $IdsInteracoes .= ','.implode(',',$IdUserNovo2);
    }
}elseif($IdUserComum2){
    $IdsInteracoes .= implode(',',$IdUserComum2);
    if($IdUserNovo2){
        $IdsInteracoes .= ','.implode(',',$IdUserNovo2);
    }
}elseif($IdUserNovo2){
    $IdsInteracoes .= implode(',',$IdUserNovo2);
}
    
asked by anonymous 20.03.2014 / 18:24

2 answers

11
There is a "hierarchy" between "online," "common," and "new," so this way you wrote is also how I would write ... But in your particular case, I see that the behavior repeats itself inside be outside each if . That is, just replace everything for only 3% with% s:

$prefixo = '';
if($IdUserOnline2){
    $IdsInteracoes .= implode(',',$IdUserOnline2);
    $prefixo = ',';
}

if($IdUserComum2){
    $IdsInteracoes .= $prefixo . implode(',',$IdUserComum2);
    $prefixo = ',';
}

if($IdUserNovo2){
    $IdsInteracoes .= $prefixo . implode(',',$IdUserNovo2);
}
    
20.03.2014 / 18:58
17

If your array keys are not repeated between them, you can do the following:

$todosArrays = $IdUserOnline2 + $IdUserComum2 + $IdUserNovo2;
$IdsInteracoes = implode(',',$todosArrays);

You can also use the array_merge function if the array keys are repeated

$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);

Remember that in the case of array_merge , check that the previous variables are arrays , avoiding errors:

$IdUserOnline2 = isset($IdUserOnline2) ? $IdUserOnline2 : array();
$IdUserComum2 = isset($IdUserComum2) ? $IdUserOnline2 : array();
$IdUserNovo2 = isset($IdUserNovo2) ? $IdUserOnline2 : array();

// É possível usar um ternário reduzido, porém é preciso inicializar as variáveis antes
// $IdUserOnline2 = $IdUserOnline2 ?: array();
// $IdUserComum2 = $IdUserComum2 ?: array();
// $IdUserNovo2 = $IdUserNovo2 ?: array();

$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);

Verifying conditions use a ternary operator .

The logic is simple: It checks if the variable exists, if it does not exist, it initializes it as a blank array . Complete Example .

PHP 7 +

A new operator was implemented in PHP 7 called Null Coalesce Operator , where you need to set a default value if the previous expression returns null.

With it, even if the variable is not set, PHP will not will generate Notice , working the same way as || of Javascript.

So we could simplify the above code to:

$IdUserOnline2 = $IdUserOnline2 ?? [];
$IdUserComum2 = $IdUserComum2 ?? [];
$IdUserNovo2 = $IdUserNovo2 ?? [];

$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);
    
20.03.2014 / 19:43