0

我尝试通过在 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!";
4

5 回答 5

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 解决方案;)

于 2011-08-25T04:03:39.793 回答
5

当你有date时,为什么需要三元或数组映射:

echo date('F', strtotime($month.'/1/2010'));

但如果您坚持使用三元组,请检查 PHP.net 以了解正确的语法。它应该是:

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

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

于 2011-08-25T04:13:24.353 回答
3

您所做的称为三元运算。典型的设置是这样的:

$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!";
于 2011-08-25T04:03:24.560 回答
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
于 2011-08-25T04:08:11.533 回答
1

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

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

但是你最好使用 switch... case。

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

以你现在的方式使用三进制的问题是你连续调用了$session_slct->f 十二次。这比 switch 昂贵得多,后者调用它一次,或者,如果你坚持使用三进制,至少先缓存变量:

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

当然,总有一些解决方案,例如:

echo date( 'F', strtotime( '01-' . $session_slct->f("theMonth") ) );
于 2011-08-25T04:05:00.410 回答