I have the following code ...

<?php
// test

$item_101_product_title = "MGT-101";  
$item_102_product_title = "MGT-102";  
$item_103_product_title = "MGT-103";  
$item_104_product_title = "MGT-104";  
$item_105_product_title = "MGT-105";  
$item_106_product_title = "MGT-106";  
$item_107_product_title = "MGT-107";  
$item_108_product_title = "MGT-108";  
$item_109_product_title = "MGT-109";  
$item_110_product_title = "MGT-110";  
$item_111_product_title = "MGT-111";  
$item_112_product_title = "MGT-112";  
$item_113_product_title = "MGT-113";  

$message = ''; 
$interval = 54;

switch(true){
    case ($interval > 0) : $message = $message . $item_101_product_title . '<br />';  
    case ($interval > 21) : $message = $message . $item_102_product_title . '<br />';  
    case ($interval > 53) : $message = $message . $item_103_product_title . '<br />';  
    case ($interval > 84) : $message = $message . $item_104_product_title . '<br />'; 
    case ($interval > 115) : $message = $message . $item_105_product_title . '<br />'; 
    case ($interval > 146) : $message = $message . $item_106_product_title . '<br />';
    case ($interval > 177) : $message = $message . $item_107_product_title . '<br />';
    case ($interval > 208) : $message = $message . $item_108_product_title . '<br />';
    case ($interval > 239) : $message = $message . $item_109_product_title . '<br />';
    case ($interval > 270) : $message = $message . $item_110_product_title . '<br />';
    case ($interval > 302) : $message = $message . $item_111_product_title . '<br />';
    case ($interval > 332) : $message = $message . $item_112_product_title . '<br />'; break;
}

echo $message;
?>

I expect the output to be ...

MGT-101
MGT-102
MGT-103

Instead, I get ....

MGT-101
MGT-102
MGT-103
MGT-104
MGT-105
MGT-106
MGT-107
MGT-108
MGT-109
MGT-110
MGT-111
MGT-112

Where did I go wrong?

Recommended Answers

All 7 Replies

You need to have a break for each case statement.

I wasn't aware that you could use logic in switch case statements. Have you tried if / else if / else blocks?

if($interval > 0)
    $message .= $item_101_product_title . '<br />';
elseif($interval > 21)
    $message .= $item_102_product_title . '<br />';
elseif($interval > 53)
    $message .= $item_103_product_title . '<br />';
elseif($interval > 84)
    $message .= $item_104_product_title . '<br />';
//...

You must have break for each case. Also, you should use variable within switch parantheses which likes switch($interval) instead switch(true).

Member Avatar for diafol

How's this?

$arr = array(
    array(101,0),
    array(102,21),
    array(103,53),
    array(104,84),
    array(105,115),
    array(106,146),
    array(107,177),
    array(108,208),
    array(109,239),
    array(110,270),
    array(111,302),
    array(112,332)
);
$interval = 54;
foreach($arr as $r){
    if($interval > $r[1])$message[] = "MGT-" . $r[0];   
}
echo implode("<br />",$message);

Gives:

MGT-101
MGT-102
MGT-103

For those who suggested breaks, I did not include the break as I wanted each successive statement to be evaluated so that the $message variable would accumulate the prior content.

I like the array solution, but I am not very knowledgeable of how they work. So when a friend sent me a solution where I eleiminated the switch statement in favor of a series of if statements, rather than the if-elseif, I used it. I did not use the elseif as it also would not accumulate the variable $message content to include all.

And, yes, you can use logical operators, but they must evaluate to either true or false only.

Thanks to all for your time and consideration.

I did not include the break as I wanted each successive statement to be evaluated

That is not what happens. As soon as the first condition evaluates to true, all code will be executed up to the first break. If you reverse your conditions, it will probably work as expected.

That is not what happens. As soon as the first condition evaluates to true, all code will be executed up to the first break. If you reverse your conditions, it will probably work as expected.

So, you are saying that I should evaluate the $interval > 302 first? I will try that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.