Jump to content

[Solved] Switch Statement Options?


maxhugen

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/112282-solved-switch-statement-options/
Share on other sites

$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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.