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

前端 未结 10 1572

What are the ? and : operators in PHP?

For example:

(($request_type == \'SSL\') ? HTTPS_SERVER : HTTP_SERVER)
10条回答
  •  情话喂你
    2020-11-22 05:05

    It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.

    It's basically a shorthand if statement, and the above code could also be rewritten as follows:

    if ($request_type == 'SSL') {
       HTTPS_SERVER;
    }
    else {
       HTTP_SERVER;
    }
    

提交回复
热议问题