Using Ternaries to find Full Month Name (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"开发者_如何学Go) == '10' ? "October" ||
$session_slct->f("theMonth") == '11' ? "November" ||
$session_slct->f("theMonth") == '12' ? "December" : "Invalid Month!";
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 ;)
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.
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!";
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
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") ) );
精彩评论