我是PHP的新手.我在WordPress中遇到了这种语法.该代码的最后一行是做什么的?
$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
$page = $page ? $page : 'default'
Run Code Online (Sandbox Code Playgroud) php wordpress ternary-operator language-construct conditional-operator
我想在EL中声明一个条件方法表达式,如下所示:
<p:dataTable id="#{cc.attrs.datatableId}" var="overview"
rowSelectListener="#{cc.attrs.detailsMode == 'single' ? cc.attrs.bean.onRowSelect : cc.attrs.bean.onRowUrlSelect}">
Run Code Online (Sandbox Code Playgroud)
但是,它抛出一个EL异常:
javax.el.ELException:不是有效的方法表达式:#{cc.attrs.detailsMode =='single'?cc.attrs.bean.onRowSelect:cc.attrs.bean.onRowUrlSelect}
我如何声明条件EL方法表达式?
int foo = bar > baz ? bar : baz;
int foo = someBoolean ? bar : baz;
int foo = (bar > baz) ? bar : baz;
int foo = (someBoolean) ? bar : baz;
int foo = (bar > baz) ? bar : baz;
int foo = someBoolean ? bar : baz;
我无法决定应使用这三个中的哪一个。我可以:
在示例中,请不要使用括号并冒着可读性差的危险,例如:
min[0] = min[0] > pos.x ? pos.x : 0;
始终使用括号,但冒着简短表达式中有些难看的代码的风险:
setValue(val + scrollBar.getBlockIncrement() * ((scrollsUp) ? -1 : 1));
当条件中有空格时,请放在两者之间,并使用括号;但如果条件只是布尔变量,则不要使用括号:
min[0] = (min[0] > …
java coding-style conventions ternary-operator conditional-operator
特定的JS解析器(不在我的控件中)不理解嵌套的条件运算符语法,如下所示:
return num === 1 ? condition ? condition : something : something;
Run Code Online (Sandbox Code Playgroud)
因此,我想用if/else块替换文件中的所有条件运算符(简单和嵌套).我该怎么办呢?(Textmate或类似的regexp会有所帮助.)
在C++中,我试图使用条件运算符进行隐式转换.考虑这个例子:
class MyFloat
{
public:
MyFloat(float val){m_val = val;}
operator float(){return m_val;}
protected:
float m_val;
};
int main(int argc, char **argv)
{
MyFloat a = 0.5f;
MyFloat b = 1.0f;
float x = true ? a-0.5f : b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会导致编译器错误:
error: operands to ?: have different types ‘MyFloat’ and ‘float’
Run Code Online (Sandbox Code Playgroud)
我希望条件运算符隐式转换b为a-0.5float 类型.但这不会发生.我如何实现这种隐式演员?
理想情况下,我想避免静态强制转换或访问方法float MyFloat::getValue().
var discount = (i == 1) ? definition.SecondPetDiscount ?? definition.AdditionalPetDiscount :
(i == 2) ? definition.ThirdPetDiscount ?? definition.AdditionalPetDiscount :
definition.AdditionalPetDiscount;
Run Code Online (Sandbox Code Playgroud)
你有没有人能给我一个这段代码的解释?我知道三元运算符.但上面不是那样的.
正常的三元运算符如下所示:
PaidDate = ( paidDate == null ? DateTime.Now : paidDate);
Run Code Online (Sandbox Code Playgroud) 这是我最近在VS2012中编写的一些代码:
///<summary>Lambda: Returns true if the field is significant within a baseline context</summary>
const auto IsSignificantBaselineField = [](const field_info & field)->bool
{
//Some lines removed here!
return something;
};
///<summary>Lambda: Returns true if the field is significant within a project context</summary>
const auto IsSignificantProjectField = [&IsSignificantBaselineField](const field_info & field)->bool
{
if (!IsSignificantBaselineField (field))
return false;
//Some lines removed here!
return something_else;
};
return std::any_of (modified_fields.begin (), modified_fields.end (), (proj_id == 0) ? IsSignificantProjectField : IsSignificantBaselineField);
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当'proj_id = 90000'时,std :: any_of正在调用IsSignificantProjectField,而我的意图是在这种情况下将调用IsSignificantBaselineField. …
我想,能够做到这一点:
var b = $(this).is(':checked')
$('.perspective-col'). (b) ? show() : hide()
Run Code Online (Sandbox Code Playgroud)
代替
var b = $(this).is(':checked')
if(b) {
$('.perspective-col').show()
} else {
$('.perspective-col').hide()
}
Run Code Online (Sandbox Code Playgroud)
我想要太多吗?或者是否有一些我还没有找到的精彩javascript语法?或者我是否认为到目前为止JQuery中不存在这样的事情?(我正在使用JQuery 1.9.0)
我在代码审查中找到了由我的团队成员编写的代码.它包含嵌套的三元运算符.我告诉他如果有多个嵌套用于可读性目的,请使用if else.我们争论过这个问题.
以下是代码
ColorEnum color = opacity == Opacity.FIVE? ColorEnum.BLACK :
opacity == Opacity.TEN? ColorEnum.WHITE :
opacity == Opacity.FIFTY? ColorEnum.RED :
opacity == Opacity.TWENTY? ColorEnum.BLUE :
opacity == Opacity.FIFTEEN? ColorEnum.PURPLE : null;
Run Code Online (Sandbox Code Playgroud)
随着新配置的出现,这段代码正在发生变化.
那么这里有什么更好的?三元运算符还是其他?
我是C的新手,在编译简单代码时遇到以下问题:
#include <stdio.h>
int main()
{
int i, a, b;
i = 3;
a = b = 0;
printf("Before: ");
printf("%d %d\n", a, b);
i == 3 ? a = 4 : a = 10; /* Line 9 */
printf("After: ");
printf("%d %d\n", a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给我错误:
#gcc some.c
In function ‘main’:
some.c:9: error: lvalue required as left operand of assignment
Run Code Online (Sandbox Code Playgroud)
我不明白.我究竟做错了什么?