Welcome to WebmasterWorld Guest from 54.147.238.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Unexpected error in switch case and concatenation

Why is the comparison operator being ignored in this switch case statement?

     
3:05 pm on Jul 13, 2012 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 11, 2005
posts: 129
votes: 0


I have the 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 to get

MGT-101
MGT-102
MGT-103


But, 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?
8:48 am on July 16, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
posts:941
votes: 0


i don't see anything wrong... strange behavour

have you tried to var_dump your variable and see what you get?
10:04 am on July 16, 2012 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Aug 13, 2003
posts:1030
votes: 0


Switch/Case ignores conditions once one of them has been met (or at least it does in the C programming language). It executes all commands until 'break' is found, even if the next condition is not met.

Once $interval>0 is found, all the code in the block is run. This is why it is adding all the titles.
11:46 am on July 16, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
posts:941
votes: 0


PCInk is correct

from php manual:
In a switch statement, the condition is evaluated only once and the result is compared to each case statement.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members