Expressions

Write conditional expressions so that they read naturally aloud. Sometimes eliminating a not operator (!) will make an expression more understandable. Use parentheses liberally to resolve ambiguity. Using parentheses can force an order of evaluation. This saves the time a reader may spend remembering precedence of operators.

Keep each line simple. The trinary operator (x ? 1 : 2) usually indicates too much code on one line. if..elseif..else is usually more readable. Don't sacrifice clarity for cleverness.

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.