Backend Development 6 min read

Understanding PHP's Ternary Operator

The article explains PHP's ternary operator, covering its basic syntax, equivalence to if‑else statements, usage examples, extensions with multiple statements, nesting, best practices, and common pitfalls, providing clear code illustrations for developers seeking concise conditional expressions.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP's Ternary Operator

This article introduces PHP's ternary operator, hoping to help interested readers.

Ternary operator syntax: condition ? result1 : result2 . The expression before the question mark is the condition; if true, result1 is returned, otherwise result2.

Let's explore it in detail.

Today I encountered a statement I couldn't understand:

<code>$if_summary = $row['IF_SUMMARY']==2?'是':'否';</code>

This statement is equivalent to:

<code>if($row['IF_SUMMARY']==2){
    $if_summary="是";
} else {
    $if_summary="否";
}</code>

The ternary operator functions like an if...else flow but is written in a single line, making the code concise and potentially more efficient.

Code format:

<code>(expr1) ? (expr2) : (expr3);</code>

Explanation: If expr1 evaluates to true, expr2 is executed; otherwise expr3 is executed.

Implementing the same logic with an if statement requires multiple lines:

<code>if(expr1) {
    expr2;
} else {
    expr3;
}</code>

Thus, the advantages of the ternary operator are evident.

In practice it is usually used for simple single‑statement cases, e.g.:

<code>$a>$b ? print "a大于b" : print "a小于b";</code>

However, the ternary operator can be extended so that each branch contains more than one statement, for example:

<code>(expr1) ? (expr2).(expr3) : (expr4).(expr5);</code>

Multiple statements can be concatenated using the string operator . , and each statement is enclosed in parentheses to denote a complete expression.

With this extension, its functionality closely mirrors an if...else flow.

The ternary operator can also be nested.

For instance, when $a>$b is true:

<code>$a>$b ? $x=($a<$c ? $c-$a : $a-$c) : $x=($b<$c ? $c-$b : $b-$c);</code>

Nested ternary operators can be hard to read and maintain, but they remain more concise than equivalent if...else structures.

For developers who favor brevity, using the ternary operator instead of if statements is an excellent choice.

The following statements are syntactically correct, showing that the second or third operand can be omitted:

<code>$a>$b ? print "Yes" : "";
$a>$b ? '' : print 'No';</code>

Note: When using the ternary operator, it is recommended to use print instead of echo .

Consider this example:

<code>$str = $_GET['abc'] ? 'wangjinbo' : 'wjb';</code>

This should not be read as "if $str equals $_GET['abc'] then assign 'wangjinbo' else 'wjb'"; equality checks require == . The correct interpretation is that if $_GET['abc'] is falsy (empty string, null, 0, undefined), $str becomes 'wjb'; otherwise it becomes 'wangjinbo'.

Note: If the condition inside an if statement is omitted, the expression itself is evaluated; a truthy $_GET['abc'] makes $str 'wangjinbo', otherwise 'wjb'. True indicates a value exists, false indicates no value.

Backend DevelopmentPHPcode simplificationternary operatorconditional expression
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.