php - Change text color based on switch value ← (PHP, HTML)

Solution:

you can try using

switch ($row['destroydate']) {

  case NULL:

    $destroydate = '<span style="color:#FF0000">No Date Set</span>'; <---MAKE RED
    //echo $destroyeddate;
    break;

  case "0000-00-00":

    $destroydate = '<span style="color:#39CD0D">No Date Set</span>'; <--- MAKE GREEN

    break;

  default:

    $destroydate = '<span style="color:#0808D8 ">'.$row["destroydate"].'</span>'; <--- MAKE BLUE

    break;

}

Answer



Solution:

You should use this below. Create a function and use it with sending the Date data. The switch will return a result, always. This way you can always use your new function at any file. If there is anything needs to be added/changed, you don't need to change all the files who are using this switch, but only have to change the function. If you prefer: you could also make it a static function (because it's only returning some html).

/**
 * @param $date
 *
 * @return string
 */
    Public function getDateColor($date)
    {
        // Check on date and return result
        switch($date):
            case(empty($date)):
                return '<span color="red">No Date Set</span>';
                break;
            case($date == 0000-00-00):
                return '<span color="Green">No Date Set</span>';
                break;
            default:
                return '<span color="blue">' . $date . '</span>';
        endswitch;
    }

Source