What are the PHP operators “?” and “:” called and what do they do?

前端 未结 10 1564

What are the ? and : operators in PHP?

For example:

(($request_type == \'SSL\') ? HTTPS_SERVER : HTTP_SERVER)
10条回答
  •  醉话见心
    2020-11-22 05:08

    This is the conditional operator.

    $x ? $y : $z
    

    means "if $x is true, then use $y; otherwise use $z".

    It also has a short form.

    $x ?: $z
    

    means "if $x is true, then use $x; otherwise use $z".

    People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

提交回复
热议问题