What is the best practice for setting checkboxes to 'checked' based on PHP variables?

When setting checkboxes to 'checked' based on PHP variables, the best practice is to use a conditional statement to check if the variable is true or false, and then output the 'checked' attribute accordingly. This can be done by echoing the 'checked' attribute within the input tag if the variable evaluates to true.

<input type="checkbox" name="example" <?php echo ($variable == true) ? 'checked' : ''; ?>>