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

前端 未结 10 1717

What are the ? and : operators in PHP?

For example:

(($request_type == \'SSL\') ? HTTPS_SERVER : HTTP_SERVER)
10条回答
  •  借酒劲吻你
    2020-11-22 05:05

    This is a short way of writing if sentences. It is also used in other languages like Java, JavaScript and others.

    Your code,

    $protocol = $request_type == 'SSL' ? HTTPS_SERVER : HTTP_SERVER;
    

    can be written like this:

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

提交回复
热议问题