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

前端 未结 10 1685

What are the ? and : operators in PHP?

For example:

(($request_type == \'SSL\') ? HTTPS_SERVER : HTTP_SERVER)
10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:09

    As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;

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

提交回复
热议问题