What are the ?
and :
operators in PHP?
For example:
(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)
The standard logical operators and, or, not, and xor are supported by PHP. Logical operators first convert their operands to boolean values and then perform the respective comparison.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With