标签: conditional-operator

PHP中的这种语法(page = $ page?$ page:'default')是什么意思?

我是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

3
推荐指数
2
解决办法
866
查看次数

EL条件方法表达式

我想在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.E​​LException:不是有效的方法表达式:#{cc.attrs.detailsMode =='single'?cc.attrs.bean.onRowSelect:cc.attrs.bean.onRowUrlSelect}

我如何声明条件EL方法表达式?

jsf facelets el conditional-operator methodexpression

3
推荐指数
1
解决办法
5821
查看次数

条件(三元)运算符代码样式

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;

我无法决定应使用这三个中的哪一个。我可以:

  1. 在示例中,请不要使用括号并冒着可读性差的危险,例如:

    min[0] = min[0] > pos.x ? pos.x : 0;

  2. 始终使用括号,但冒着简短表达式中有些难看的代码的风险:

    setValue(val + scrollBar.getBlockIncrement() * ((scrollsUp) ? -1 : 1));

  3. 当条件中有空格时,请放在两者之间,并使用括号;但如果条件只是布尔变量,则不要使用括号:

    min[0] = (min[0] > …

java coding-style conventions ternary-operator conditional-operator

3
推荐指数
1
解决办法
5826
查看次数

用if/else自动替换条件运算符?

特定的JS解析器(不在我的控件中)不理解嵌套的条件运算符语法,如下所示:

return num === 1 ? condition ? condition : something : something;
Run Code Online (Sandbox Code Playgroud)

因此,我想用if/else块替换文件中的所有条件运算符(简单和嵌套).我该怎么办呢?(Textmate或类似的regexp会有所帮助.)

javascript regex replace conditional-operator

3
推荐指数
1
解决办法
1360
查看次数

C++中的隐式转换

在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().

c++ casting conditional-operator implicit-conversion

3
推荐指数
1
解决办法
1881
查看次数

有条件的算子?

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)

.net c# ternary-operator conditional-operator

3
推荐指数
1
解决办法
757
查看次数

在lambda上使用条件运算符调用std :: any_of会产生意外结果

这是我最近在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. …

c++ lambda conditional-operator c++-standard-library c++11

3
推荐指数
1
解决办法
1024
查看次数

方法调用的Javascript三元运算符

我想,能够做到这一点:

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)

javascript jquery conditional-operator

3
推荐指数
2
解决办法
2037
查看次数

嵌套的三元运算符vs嵌套if else,这在可读性方面更好

我在代码审查中找到了由我的团队成员编写的代码.它包含嵌套的三元运算符.我告诉他如果有多个嵌套用于可读性目的,请使用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)

随着新配置的出现,这段代码正在发生变化.

那么这里有什么更好的?三元运算符还是其他?

language-agnostic ternary-operator conditional-operator

3
推荐指数
2
解决办法
343
查看次数

获取错误"左值作为赋值的左值"

我是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)

我不明白.我究竟做错了什么?

c compiler-errors ternary-operator conditional-operator

3
推荐指数
1
解决办法
165
查看次数