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

时间:2011-08-25 04:00:19

标签: php if-statement short

我尝试在php中使用此技术来更改值,但它不起作用!不幸的是,我也不知道这种技术的名称。因此限制我在谷歌搜索解决方案。

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 个答案:

答案 0 :(得分:5)

我想你想要:

// $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!";

甚至:

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;

但这些不是DRY选项,您可以使用PhpMyCoder和efritz解决方案;)

答案 1 :(得分:5)

为什么在date

时需要三元组或数组映射
echo date('F', strtotime($month.'/1/2010'));

但如果您坚持使用三元组,请查看PHP.net以查找correct syntax。它应该是:

echo $month == '01' ? 'January' :
     $month == '02' ? 'February' :
     //etc

基本上,||是OR运算符,而不是您需要为三元组指定备用项的冒号。

答案 2 :(得分:3)

您正在做的事情称为ternary operation。典型的设置是:

$variable = ($someValue == "abc") ? "yes" : "no";

我不确定你为什么使用管道代替冒号。这样做:

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!";

答案 3 :(得分:3)

Xaerxees的回答是正确的,但更好的方式将是:

$months = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
);

if (isset($months[$session_slct->f("theMonth") - 1])) {
    echo $months[$session_slct->f("theMonth") - 1];
} else {
    echo "Invalid Month";
}

或者,如果您愿意,可以随时将它们编入索引,并删除- 1内容:

$months = array(
    '01' => 'January',
    '02' => 'February',
    '03' => 'March',
    // etc

答案 4 :(得分:1)

您尝试使用三元运算符,但是您使用的是||而不是:

$session_slct->f("theMonth") == '01' ?'January':
$session_slct->f("theMonth") == '02' /* ... 

但你最好使用开关......案例。

$month = 'Invalid Month!';
switch( $session_slct->f("theMonth") )
{
   case '01':
      $month = 'January';
      break;
   case '02':
      $month = 'February';
      break;
   /* ... */
}

使用三元组的问题在于您连续拨打$session_slct->f 十二次。这远比交换机贵一点,它只调用一次,或者,如果你坚持三元,至少先缓存变量:

$month = $session_slct->f("theMonth");
echo $month == '01' ?'January':
     $month == '02' ?'February':// yada yada yada/

当然总会有以下解决方案:

echo date( 'F', strtotime( '01-' . $session_slct->f("theMonth") ) );