Is there an efficient way to write this PHP if / else statement?
Posted
by
nvoyageur
on Stack Overflow
See other posts from Stack Overflow
or by nvoyageur
Published on 2011-01-12T20:39:58Z
Indexed on
2011/01/12
20:53 UTC
Read the original article
Hit count: 212
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; ?>
© Stack Overflow or respective owner