homepage Welcome to WebmasterWorld Guest from 54.145.183.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Unexpected error in switch case and concatenation
Why is the comparison operator being ignored in this switch case statement?
calvinmicklefinger

5+ Year Member



 
Msg#: 4475411 posted 3:05 pm on Jul 13, 2012 (gmt 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?

 

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4475411 posted 8:48 am on Jul 16, 2012 (gmt 0)

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

have you tried to var_dump your variable and see what you get?

PCInk

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4475411 posted 10:04 am on Jul 16, 2012 (gmt 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.

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4475411 posted 11:46 am on Jul 16, 2012 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved