Succinct conditional structure with multiple comparisons

Asked

Viewed 81 times

-1

How to compare three items with the same result? I’m currently doing it as follows, only this way the code design doesn’t look stylish.

if ($title == 0 && $squad == 0 && $level == 0) {
   return true;
}

What I’m looking for is something like:

if ($title && $squad && $level == 0) {
   return true;
}
  • Try $title == $squad && $title == $level

  • 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

  • You cannot have more than two conditions. PHP will generate the error T_IS_EQUAL.

  • The form suggested by @Valdeirpsr may fail if $title is different from 0. If you want to use it like this, you can add the validation of $title == 0

  • I didn’t understand @rLinhares

  • 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 always 0.

  • Only it gives shape that you suggested an error was presented. @rLinhares

  • You can use the second alternative? return $title == $squad == $level == 0;

  • I just don’t want to use it as a return. I want to use it in a @rLinhares conditional structure

  • everything has to be the same 0? or just be the same??

  • ALL EQUAL TO ZERO. @rLinhares

  • Values for variables are only 0 or 1? or may have other values?

Show 8 more comments

4 answers

5

If the if continue on things that are not a return false:

if (!$a && !$b && !$c) {
   return true;
}

... outros statements ...

Alternative to the whole question code, in PHP, if it will return of qq way:

return !$a && !$b && !$c;

But the "inelegant" is relative, I find much worse compact that is not readable, than clear as the original.

Just be careful with PHP. Depending on the type, because strange things happen, especially when you "invent fashion".

Note: as mentioned by @rray in the comments, this expression is equivalent to the top:

if ( !($a || $b || $c) ) ...
  • 1

    !($a || $b || $c) would be an equivalent (if I didn’t get lost) ?

  • 1

    @I thought about it too, but I thought it would get weird in the if (!( ... ))

3


You have put the tags PHP and Java, so I will give solutions in both languages.

In PHP:

$t = array_unique(array($title, $squad, $level));
if (count($t) == 1 && in_array(0, $t)) {
    return true;
}

In Java:

Set<Integer> s = Set.of(title, squad, level);
if (s.size() == 1 && s.contains(0)) {
    return true;
}

Other in Java:

if (IntStream.of(title, squad, level).allMatch(x -> x == 0)) {
    return true;
}

1

Our friend Miguel formulated the following answer, but for some reason he did not want to post:

<?php

$title = 0;
$squad = 0;
$level = 0;

var_dump([$title, $squad, $level] == [0,0,0]);

$level = 1;

var_dump([$title, $squad, $level] == [0,0,0]);

See here working on ideone.

  • Obgado Victor, I’m on the phone and I was already leaving work and I was going to be without computer. Yours is much better

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.