Aaron Shoudy
Aaron Shoudy

Reputation: 33

Evaluate a value in a switch block using comparisons in each case

Suppose I have a variable integer and wish to do different things if the value is greater then one value and less then another value. My objective for this switch, is basically to send different results based on the value of $chance, which I will have many cases in the end as this is for a game.

switch ($chance)
{
    case ($chance < 15):
        echo"strike<br>";
    case ($chance < 15 && $chance > 50):
        echo"Ball<br>";
        break;
    case ($chance < 50 && $chance > 100):
        echo"Single<br>";
        break;
    case ($chance <= 150 && $chance >= 100):
        echo"double<br>";
        break;
    case ($chance <= 175 && $chance >= 151):
        echo"triple<br>";
        break;
    case ($chance > 200 && $chance > 175):
        echo"Ground Rule Double<br>";
        break;
    case ($chance < 200):
        echo"Home Run<br>";
        break;
}

Now, I've been told that I can use conditionals in switch statements, and I've also been told that I should not use them. I really don't know who to believe.

What I do know, is that currently, this switch statement does not work as intended. It doesn't generate syntax errors, but I will get random echos back. This happens when sometimes the chance may be 100 and I will get a home run echo. I'm not sure.

I know I could do the same with a series of if but it would amount to a huge difference in length of code if I can achieve the same results.

I imagine I can do something like

case 1:
 echo this
case 2:
 echo that
etc etc

Until I hit 2 or 300 but I would like to avoid that if possible.

Upvotes: 1

Views: 911

Answers (5)

mickmackusa
mickmackusa

Reputation: 47904

Despite some mathematical/comparison issues in your code, it seems that you intend to set numeric ranges that correspond to specific outcomes. It also seems inappropriate to reach multiple outcomes in a single script execution.

Given these truths, you should use an if-elseif-else block. It will be more appropriate and less verbose than a switch block.

The ONLY advantage to using a switch block is when you need to make 1 evaluation and take different actions based on that evaluation. Because you are making multiple evaluations, there is no advantage in using a switch -- in fact, the computational complexity will be the same and the syntax will either be of equal or greater length versus an if block. I personally have such a distaste for switch blocks that I endeavor to use any other viable technique instead. Switch blocks only make very rare appearances in my professional projects.

By progressing in an ASC or DESC fashion, you will only need one expression in each condition. Using consistent operators in each condition will make your script easier to maintain/extend.

Finally, in PHP, elseif is one word not two.

Code:

if ($chance < 0) {
    $result = 'Balk';     // ;)
} elseif ($chance < 15) {
    $result = 'Strike';
} elseif ($chance < 50) {
    $result = 'Ball';
} elseif ($chance < 100) {
    $result = 'Single';
} elseif ($chance < 150) {
    $result = 'Double';
} elseif ($chance < 175) {
    $result = 'Triple';
} elseif ($chance < 200) {
    $result = 'Ground Rule Double';
} else {
    $result = 'Home Run';
}
echo "$result<br>";

Upvotes: 0

Mohammad Aghayari
Mohammad Aghayari

Reputation: 1160

You need just set true in you switch. Use switch (true) instead of switch ($chance).

now your code will be:

switch (true)
{
    case ($chance < 15):
        echo"strike<br>";
    case ($chance < 15 && $chance > 50):
        echo"Ball<br>";
        break;
    case ($chance < 50 && $chance > 100):
        echo"Single<br>";
        break;
    case ($chance <= 150 && $chance >= 100):
        echo"double<br>";
        break;
    case ($chance <= 175 && $chance >= 151):
        echo"triple<br>";
        break;
    case ($chance > 200 && $chance > 175):
        echo"Ground Rule Double<br>";
        break;
    case ($chance < 200):
        echo"Home Run<br>";
        break;
}

Upvotes: 0

Ziarno
Ziarno

Reputation: 7572

This is not how you use the switch statement. This is an example of a correct way:

switch ($a) {
  case 1:
    echo 1;
    break;
  case 2:
    echo 2; 
    break;
  default:
    echo 0;
}

For what you want to accomplish you need to use the old if-else statements.

if ($chance < 15)
    echo"strike<br>";
else if ($chance >= 15 && $chance < 50)
    echo"Ball<br>";
else if ($chance >= 50 && $chance < 100)
    echo"Single<br>";
else if ($chance <= 150 && $chance >= 100)
    echo"double<br>";
else if ($chance <= 175 && $chance >= 151)
    echo"triple<br>";
else if ($chance < 200 && $chance > 175)
    echo"Ground Rule Double<br>";
else if ($chance <= 200)
    echo"Home Run<br>";

Upvotes: 2

Mike Brant
Mike Brant

Reputation: 71384

To do what you are wanting to do, you should use an if-else. The value used for the case expression must be an integer, floating-point decimal, or string.

Upvotes: 0

Mehdi Karamosly
Mehdi Karamosly

Reputation: 5438

Switch statement syntax:

http://php.net/manual/en/control-structures.switch.php

<?php
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
       echo "i is not equal to 0, 1 or 2";
}
?>

Upvotes: 0

Related Questions