Switch statement php



  • How would I say in this switch statement Pisces displays at X number of days in January and Aries at some other day in the same month? Dahnus is default for January and should disappear when replaced by the others on the specified days.

    There will be 24 months in this switch all having replacement switch definitions.

    <?php
    // date command to find out the day of the week
    $date = date('F');
    // our switch statement will assess which day of the week it is and
    // assign our $content as assigned.
    switch ($date) {
      case 'Jan':
        $content = "Dahnus";
    //$content = " Pisces";
    //$content = " Aries";
        break;
      case 'Feb':
        $content = "Aquarius";
    //$content = " Virgo";
    //$content = " Kanya";
    
        break;
      
    }
    
    // display our content regardless of day 
    echo $content;
    ?>


  • Hey again @MPG-Radio

    I am afraid I am still confused on what is being asked. Do you mind clarifying?



  • @MPG-Radio

    I took a look at your problem again. If I understood you correctly couldn't you just use an if statement inside each switch case? So depending on the day within the month return a certain value. But again, I am not sure if I understood your question correctly.



  • Yes, a if statement sounds like the option, I think something like this....
    Here’s how I think I might approach it:

    // get the date in an easy to compare format: Jan 1st is 101, Dec 31st 1231
    $date = date('n') * 100 + date('j');

    if($date < 124) {
    $content = "between jan 1 and jan 23;
    } elseif($date < 215) {
    $content = "between jan 24 and feb 15";
    }

    // ... repeat for each grouping I want?



  • What about an if statement within each switch case?

    switch ($date) {
      case 'Jan':
        $content = "Dahnus";
        if(CONDITION HERE)  {  
               EXECUTE CODE HERE
          }
    //$content = " Pisces";
    //$content = " Aries";
        break;
      case 'Feb':
    

Log in to reply