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

发布于 2024-12-01 04:57:06 字数 845 浏览 2 评论 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!";

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

絕版丫頭 2024-12-08 04:57:06

我认为你想要:

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

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 ;)

只是在用心讲痛 2024-12-08 04:57:06

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

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

但是如果你坚持使用三元数,请检查 PHP .net 获取正确语法。它应该是:

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

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

Why do you need ternaries or array maps when you have date:

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

But if you insist on using ternaries, check PHP.net for the correct syntax. It should be:

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

Basically, || is the OR operator, not the colon you need to specify the alternative for the ternary.

在风中等你 2024-12-08 04:57:06

您所做的称为<​​a href="http://en.wikipedia.org/wiki/Ternary_operation" rel="nofollow">三元运算。典型的设置是这样的:

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

What you are doing is called a ternary operation. The typical setup is this:

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

I am not sure why you are using pipes instead of the colon. Do it like so:

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!";
醉态萌生 2024-12-08 04:57:06

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

Xaerxees's answer is correct, but a better way would be:

$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";
}

Or, if you prefer, you could always index them as the following, and drop the - 1 stuff:

$months = array(
    '01' => 'January',
    '02' => 'February',
    '03' => 'March',
    // etc
﹉夏雨初晴づ 2024-12-08 04:57:06

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

$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 昂贵得多,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") ) );

You' trying to use the ternary operator, but you're using || instead of :.

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

But you're better off using switch... case.

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

The problem with using the ternary the way you are is that you are calling $session_slct->f twelve times in a row. That is far more expensive than switch, which calls it once or, if you're insistent on ternary, at least cache the variable first:

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

Of course there are always solutions like:

echo date( 'F', strtotime( '01-' . $session_slct->f("theMonth") ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文