Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.