Alex Rivera | Logout

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

Asked 2009-07-03T17:27:15.447
52

What are the ? and : operators in PHP?

For example:

(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)
Edit
Report

1 Answer

3

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;
answered 2009-07-03T17:50:21.083

Your Answer