dpwjx32578146 2011-08-25 04:00
浏览 57
已采纳

使用三元组查找完整月份名称(PHP)

I try to change the value by using this technique in php, but it didn't work! Unfortunately, I also don't know the name of this technique. So limitation for me to search the solution in google.

echo $session_slct->f("theMonth") == '01' ? "January" ||
     $session_slct->f("theMonth") == '02' ?  "February" ||
     $session_slct->f("theMonth") == '03' ?  "March" || 
     $session_slct->f("theMonth") == '04' ?  "April" || 
     $session_slct->f("theMonth") == '05' ?  "May" || 
     $session_slct->f("theMonth") == '06' ?  "June" || 
     $session_slct->f("theMonth") == '07' ?  "July" || 
     $session_slct->f("theMonth") == '08' ?  "August" || 
     $session_slct->f("theMonth") == '09' ?  "September" || 
     $session_slct->f("theMonth") == '10' ?  "October" || 
     $session_slct->f("theMonth") == '11' ?  "November" || 
     $session_slct->f("theMonth") == '12' ?  "December"  : "Invalid Month!";
  • 写回答

5条回答 默认 最新

  • duandaishi9268 2011-08-25 04:03
    关注

    I think you wanted:

    // $month_num is in separate variable in case $session_slct->f("theMonth") is i.e. slow operation or using external resource
    $month_num = $session_slct->f("theMonth");
    echo ($month_num == '01') ? "January" :
         ($month_num == '02') ?  "February" :
         ($month_num == '03') ?  "March" :
         ($month_num == '04') ?  "April" :
         ($month_num == '05') ?  "May" :
         ($month_num == '06') ?  "June" :
         ($month_num == '07') ?  "July" :
         ($month_num == '08') ?  "August" :
         ($month_num == '09') ?  "September" :
         ($month_num == '10') ?  "October" :
         ($month_num == '11') ?  "November" :
         ($month_num == '12') ?  "December"  : "Invalid Month!";
    

    or even:

    switch ($session_slct->f("theMonth")) {
        case '01': $month = "January"; break;
        case '02': $month = "February"; break;
        case '03': $month = "March"; break;
        case '04': $month = "April"; break;
        case '05': $month = "May"; break;
        case '06': $month = "June"; break;
        case '07': $month = "July"; break;
        case '08': $month = "August"; break;
        case '09': $month = "September"; break;
        case '10': $month = "October"; break;
        case '11': $month = "November"; break;
        case '12': $month = "December"; break;
        default: $month = "Invalid Month!";
    }
    
    echo $month;
    

    but these are not really DRY options, you might use PhpMyCoder and efritz solutions ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?