maxhugen Posted June 28, 2008 Share Posted June 28, 2008 I've been searching unsuccessfully for any optional operators that can be used in the case of a switch statement. I need to return a message, according to the number of days left in a Trial subscription. For example: $days_left = $date_expiry - $date_today; switch ($days_left) { case >3: $msg = "You have ".$days_left." of your Trial."; break; case >0: $msg = "Warning: You only have ".$days_left." of your Trial."; break; case 0: $msg = "Warning: Today is the last day of your Trial."; break; case <0: $msg = "Your free Trial has expired. Please subscribe"; break; } This doesn't work (and nothing I've read suggests it should), so how could I accomplish something like this pls? MTIA Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/ Share on other sites More sharing options...
awpti Posted June 28, 2008 Share Posted June 28, 2008 You'll need to use conditionals for this - switch doesn't work the way you want. Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/#findComment-576449 Share on other sites More sharing options...
whizard Posted June 28, 2008 Share Posted June 28, 2008 $days_left = $date_expiry - $date_today; if($days_left > 3) { $msg = "You have ".$days_left." of your Trial."; } elseif($days_left < 3 && $days_left > 0) { $msg = "Warning: You only have ".$days_left." of your Trial."; } elseif($days_left = 0) { $msg = "Warning: Today is the last day of your Trial."; } else { $msg = "Your free Trial has expired. Please subscribe"; } HTH Dan Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/#findComment-576453 Share on other sites More sharing options...
maxhugen Posted June 28, 2008 Author Share Posted June 28, 2008 Sorry, unsure what you mean by conditionals... do you mean if ... elseif ... elseif ... etc? Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/#findComment-576454 Share on other sites More sharing options...
maxhugen Posted June 28, 2008 Author Share Posted June 28, 2008 OK, got it! Thanks awpti and wizard! Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/#findComment-576457 Share on other sites More sharing options...
kenrbnsn Posted June 28, 2008 Share Posted June 28, 2008 I realize this is marked as "Solved", but there is another way of doing this: <?php $days_left = $date_expiry - $date_today; switch (true) { case ($days_left > 3): $msg = "You have ".$days_left." of your Trial."; break; case ($days_left > 0): $msg = "Warning: You only have ".$days_left." of your Trial."; break; case ($days_left == 0): $msg = "Warning: Today is the last day of your Trial."; break; case ($days_left < 0): $msg = "Your free Trial has expired. Please subscribe"; break; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/#findComment-576497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.