使用Ternaries查找完整的月份名称(PHP)
我尝试在php中使用此技术来更改值,但没有成功!不幸的是,我也不知道这种技术的名称。所以我只能在谷歌上搜索解决方案使用Ternaries查找完整的月份名称(PHP),php,if-statement,short,Php,If Statement,Short,我尝试在php中使用此技术来更改值,但没有成功!不幸的是,我也不知道这种技术的名称。所以我只能在谷歌上搜索解决方案 echo $session_slct->f("theMonth") == '01' ? "January" || $session_slct->f("theMonth") == '02' ? "February" || $session_slct->f("theMonth") == '03' ? "March" || $sess
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!";
你正在做的事被称为a。典型的设置如下:
$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!";
我想你想要:
// $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;
但这些并不是真正的选项,您可以使用PhpMyCoder和efritz解决方案;) 您试图使用三元运算符,但您使用的是
|
而不是:
$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;
/* ... */
}
以这种方式使用三元组的问题在于,您连续调用了12次$session\u 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") ) );
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
当您有以下情况时,为什么需要三元组或阵列映射: 但是,如果您坚持使用ternaries,请查看PHP.net以了解更多信息。应该是:
echo $month == '01' ? 'January' :
$month == '02' ? 'February' :
//etc
基本上,
|
是OR运算符,而不是您需要为三元变量指定替代项的冒号。开关?使用数组映射。另外,你的问题标题是:让它具有描述性,而不是无用。谢谢你的回答,但你的解决方案总是去十二月,删除三元运算并使用开关功能。成功了!谢谢:)我尝试将其设置为或,这就是我使用| |的原因,而不是:三值运算既需要?
又需要:
,因此除了三值运算之外,您不能使用或管道。这是比通过12个案例使用if/case/trimal更好的解决方案;)@埃弗里茨:你想要$session\u slct->f(“theMonth”)-1
而不是$session\u slct->f(“theMonth”)+1
,如果你使用$zero\u-based\u-month=$session\u slct->f(“theMonth”)-1,它的可读性会更好;如果(在数组中($0基于月,$months))
echo $month == '01' ? 'January' :
$month == '02' ? 'February' :
//etc