PHP – Use Ternary Operators

The benefit of the ternary operator is debatable (there’s only one, by the way). Here is a line of code from an audit we performed recently:

<?php
	 
	$host = strlen($host) > 0 ? $host : htmlentities($host);
	 
?>

The ternary operator may be fine for one-liners, prototypes, and templates, but we strongly believe that an ordinary conditional statement is almost always better. PHP is descriptive and verbose. We think code should be, too.

Leave a Reply

Your email address will not be published. Required fields are marked *

*