使用三元组查找完整的月份名称 (PHP)
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你想要:
甚至:
但这些并不是真正的 DRY 选项,你可以使用PhpMyCoder 和 efritz 解决方案;)
I think you wanted:
or even:
but these are not really DRY options, you might use PhpMyCoder and efritz solutions ;)
当你有 date 时,为什么还需要三元数或数组映射:
但是如果你坚持使用三元数,请检查 PHP .net 获取正确语法。它应该是:
基本上,
||
是 OR 运算符,而不是指定三元替代项所需的冒号。Why do you need ternaries or array maps when you have date:
But if you insist on using ternaries, check PHP.net for the correct syntax. It should be:
Basically,
||
is the OR operator, not the colon you need to specify the alternative for the ternary.您所做的称为<a href="http://en.wikipedia.org/wiki/Ternary_operation" rel="nofollow">三元运算。典型的设置是这样的:
我不确定为什么你使用管道而不是冒号。这样做:
What you are doing is called a ternary operation. The typical setup is this:
I am not sure why you are using pipes instead of the colon. Do it like so:
Xaerxees 的答案是正确的,但更好的方法是:
或者,如果您愿意,您可以始终按如下所示对它们进行索引,并删除
- 1
内容:Xaerxees's answer is correct, but a better way would be:
Or, if you prefer, you could always index them as the following, and drop the
- 1
stuff:您尝试使用三元运算符,但使用的是
||
而不是:
。但你最好使用 switch...case。
按照您现在的方式使用三元的问题是您连续调用
$session_slct->f
十二次。这比 switch 昂贵得多,switch 调用它一次,或者,如果你坚持使用三元,至少首先缓存变量:当然总是有类似的解决方案:
You' trying to use the ternary operator, but you're using
||
instead of:
.But you're better off using switch... case.
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:Of course there are always solutions like: