Is there an efficient way to write this PHP if / else statement?
- by nvoyageur
I've written a simple issue tracker for my web app. I have some comments that I want to keep private (only a role of 'root' can see them). Is there a better way to write the following so I do not need the empty else section?
$role will be 'root' or some other values
$is_private will be true if the comment is private
<?php
// Don't show private comments to non-root users
if ($is_private && 'root' != $role):
// NON Root cannot see private
else:
?>
<div class="comment <?= $is_private ? 'private' : '' ; ?>">
<div class="comment-meta toolbar">
<?= $is_private ? 'PRIVATE - ': ''; ?>
<span class="datestamp"><?= $created_at; ?></span> -
<span class="fullname"><?= $fname . ' ' . $lname; ?></span></div>
<p class="content"><?= nl2br($body); ?></p>
</div>
<?php endif; ?>