Function simulating if in php

Asked

Viewed 41 times

0

It would be possible to minimize or make a function that has the same functioning of this code below?

                        if($rank <= 50){ 
                            echo 'Descolorido';
                        }
                        else if($rank <= 100){ 
                            echo 'Azul Bebê';
                        }
                        else if($rank <= 200){ 
                            echo 'Roxo Bacon';
                        } 
                        else if($rank <= 300){ 
                            echo 'Verde ET';
                        } 
                        else if($rank <= 500){ 
                            echo 'Marrom Madeira';
                        } 
                        else if($rank <= 700){ 
                            echo 'Laranja Power';
                        } 
                        else if($rank <= 900){ 
                            echo 'Vermelho Killer';
                        } 
                        else if($rank <= 1500){ 
                            echo 'Rosa Piriguete';
                        } 
                        else if($rank <= 1700){ 
                            echo 'Amarelo Bobba';
                        } 
                        else if($rank <= 2000){ 
                            echo 'Turquesa Maravilha';
                        } 
                        else if($rank <= 2300){ 
                            echo 'Gótica Má';
                        } 
                        else if($rank <= 2500){ 
                            echo 'Ametista Destruidor';
                        } 
                        else if($rank <= 2700){ 
                            echo 'Esmeralda Divindade';
                        } 
                        else if($rank <= 3000){ 
                            echo 'Púrpura Star';
                        } 
                        else if($rank <= 3500){ 
                            echo 'Dourado Rei';
                        } 
                        else if($rank >= 3500){ 
                            echo 'Rainbow';
                        } 

1 answer

2


Move the comparisons to a loop and map with a array rules can become more readable:

function determineRank($input) {
    // valor retornado caso $input seja maior que todos os $ranks
    $default = 'Rainbow';
    $ranks = [
        50 => 'Descolorido',
        100 => 'Azul Bebê',
        200 => 'Roxo Bacon',
        300 => 'Verde ET',
        500 => 'Marrom Madeira',
        700 => 'Laranja Power',
        900 => 'Vermelho Killer',
        1500 => 'Rosa Piriguete',
        1700 => 'Amarelo Bobba',
        2000 => 'Turquesa Maravilha',
        2300 => 'Gótica Má',
        2500 => 'Ametista Destruidor',
        2700 => 'Esmeralda Divindade',
        3000 => 'Púrpura Star',
        3500 => 'Dourado Rei',
    ];

    foreach ($ranks as $rank => $value) {
        if ($input <= $rank) {
            return $value;
        }
    }

    return $default;
}

// Exemplo
$input = rand(45, 3600);
echo $input . ' - ' . determineRank($input) . PHP_EOL;

Per function box rand() all made that run the script it will generate a random number of 45 until 3600 (a range that encompasses all possible values) and write what value and its return

See in execution in 3v4l.org. At the end I looped to call the function 20 times with different values:

1873 - Turquesa Maravilha 
2440 - Ametista Destruidor 
1947 - Turquesa Maravilha 
1263 - Rosa Piriguete 
3344 - Dourado Rei 
2266 - Gótica Má 
516 - Laranja Power 
188 - Roxo Bacon 
2865 - Púrpura Star 
1052 - Rosa Piriguete 
2403 - Ametista Destruidor 
3118 - Dourado Rei 
2248 - Gótica Má 
2335 - Ametista Destruidor 
738 - Vermelho Killer 
1021 - Rosa Piriguete 
2026 - Gótica Má 
1733 - Turquesa Maravilha 
289 - Verde ET 
2094 - Gótica Má
  • I didn’t quite understand how to display the rank with a certain amount of messages.

  • I think you got it a little wrong. The function is based on for example: determineRank($quantityand messages) and will display the Tarja equivalent to that amount. For example: <= 50 is Discolored would look like this in theory (I suppose I have 45 messages): determineRank(45) and display Discolored, because 45 is less than 50 (<=)

  • 1

    I believe his code should be determineRank($input) and so does exactly what you need.

  • 1

    Thanks @Andersoncarloswoss. Rand() was going to generate a random value every time, so it wasn’t beating :)

  • 1

    @Paulosérgiofilho corrected the example. The logic is correct, only the example that was wrong

  • I got it, thank you very much!

  • I had tried before to use the function directly as @Andersoncarloswoss said, but it had not worked. Probably had done something wrong! :)

Show 2 more comments

Browser other questions tagged

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