What are some best practices for improving the readability of ternary operators in PHP code?

Ternary operators can sometimes make code hard to read, especially when they are nested or contain complex conditions. To improve readability, it is recommended to break down complex ternary operators into multiple lines, use parentheses to clarify the order of operations, and avoid nesting too many ternary operators within each other.

// Original ternary operator
$result = ($condition1) ? ($value1) : (($condition2) ? ($value2) : ($value3));

// Improved readability with multiple lines and parentheses
$result = ($condition1)
    ? $value1
    : (
        ($condition2)
        ? $value2
        : $value3
    );