If it is something constant in the project, I would recommend creating a function for it. Even, it would be possible, in addition to the suggestions in the other answers, to use the function array_reduce
:
function has_same_value($array, $value) {
return array_reduce($array, function ($carry, $item) use ($value) {
return $carry && ($item == $value);
}, true);
}
So just do it:
return has_same_value([$title, $squad, $level], 0);
See working on Repl.it | Ideone
Or else:
function array_all($array, $callback) {
foreach($array as $item) {
if (!$callback($item)) {
return false;
}
}
return true;
}
Which benefits from short-circuiting logical expressions, stopping iterating on the first fake item.
$title = 0;
$squad = 0;
$level = 0;
$resultado = array_all([$title, $squad, $level], function ($item) {
return $item == 0;
});
var_dump($resultado); // bool(true)
See working on Repl.it | Ideone
Try
$title == $squad && $title == $level
– Valdeir Psr
I’ve seen this kind of comparison, but I don’t want something like that. I want it the way I described it up @Valdeirpsr
– Ikaro Sales
You cannot have more than two conditions. PHP will generate the error T_IS_EQUAL.
– Valdeir Psr
The form suggested by @Valdeirpsr may fail if
$title
is different from0
. If you want to use it like this, you can add the validation of$title == 0
– rLinhares
I didn’t understand @rLinhares
– Ikaro Sales
I am understanding that the three variables need to be equal to
0
. If so, you can compare how @Valdeirpsr suggested it (this will be true if it’s all 0, 1, 2 or any value) and add what I put in, ensuring it’s always0
.– rLinhares
Only it gives shape that you suggested an error was presented. @rLinhares
– Ikaro Sales
You can use the second alternative?
return $title == $squad == $level == 0;
– rLinhares
I just don’t want to use it as a return. I want to use it in a @rLinhares conditional structure
– Ikaro Sales
everything has to be the same
0
? or just be the same??– rLinhares
ALL EQUAL TO ZERO. @rLinhares
– Ikaro Sales
Let’s go continue this discussion in chat.
– rLinhares
Values for variables are only
0
or1
? or may have other values?– rray