switch does not show the expected result

Asked

Viewed 151 times

-5

What’s wrong? It was supposed to appear "no value", but when it starts with zero appears "very high value" in the message.

<?php
$num = 0;

switch ($num){
    case($num>100);
        echo'valor muito alto';
        break;
    case($num<80 && $num>51);
        echo'valor medio';
        break;
    case($num==50);
        echo'valor perfeito';
        break;
    case($num<=10);
        echo'valor muito baixo';
        break;
    case($num==0);
        echo'sem valor';
        break;
}
?>
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

12

The case does not work the way you expect. It is not a substitute for the if. See the documentation of him. The case accepts only one value, it only tests the equality of that value.

More or less what you want is this:

$num = 0;
if ($num > 100) echo 'valor muito alto';
else if ($num < 80 && $num > 51) echo 'valor medio';
else if ($num == 50) echo 'valor perfeito';
else if ($num <= 10) echo 'valor muito baixo';
else if ($num == 0) echo'sem valor';

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Still, there’s a logic error there. You can’t even understand what the goal is. It has bands of values overlapping, ranges of values that are not reached by any of the conditions.

  • The way you’re going, you’ll get the first reversal medal of the site :)

0

I tested here and it worked, is a gambiarra, but meets your need, as I want to test more of a value in the case, I have to create a array, as @Maniero said, the case doesn’t work that way where you want, but you can do a gambiarra.

Creating 3 arrays with the range of values you want, and testing if the value exists within the array using the function in_array, looks nice...

<?php
    $num = 40;

    $array100_80 = array();
    $array79_51 = array();
    $array49_1 = array();

    for ($i = 80; $i <= 100; $i++) {
        $array100_80[$i] = $i;
    }

    for ($i = 51; $i <= 79; $i++) {
        $array79_51[$i] = $i;
    }

    for ($i = 1; $i <= 49; $i++) {
        $array49_1[$i] = $i;
    }

    switch ($num){
        case(0):
            echo'sem valor';
            break;
        case(in_array($num, $array100_80)):
            echo'valor muito alto';
            break;
        case(in_array($num, $array79_51)):
            echo'valor medio';
            break;
        case(50):
            echo'valor perfeito';
            break;
        case(in_array($num, $array49_1)):
            echo'valor muito baixo';
            break;
    }
  • It doesn’t work. It doesn’t even spin. Even if it does, it wouldn’t work because there are three cases testing the same value, true.

  • I tested and ran kkkkkkk O.o, detail, tried to change the value of $num? Details2, saw that I changed the man function?

  • 1

    This last edition improved but I do not know if it would always work, I will not waste time in testing each situation, after all this is something so wrong to do, it is so without purpose that is not worth the time. But it still has Warning.

  • What old Warning is that? Here in the company I work disable the Warning’s server, I do not see any error, as I said, it is not the right thing to do, it is a scam, but if there is no better option, it is what you have for hj right? rs

  • 1

    I don’t think that’s a decent way to use a Switch/Case. In fact in several Clean-code materials it is asked to avoid the use to the maximum. But anyway, maybe it works.

  • 1

    IS syntax error. Has ; where there should be :. Wheel but is wrong. Congratulations for disabling Warning.

  • kkkkk am not I who define the company policy where I work @bigow, I wish it was, but anyway.... I gave one last corrected for love of not giving more pal ai to Voce

  • 1

    Ah, and just one thing, now that I’ve seen, my syntax error was due to the user rs syntax error, I copied and pasted his code to take the test......

Show 3 more comments

Browser other questions tagged

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