Reputation:
How do I hide the html tags when the user is not logged on for the users name to be display?
<li><a href="#" title="#"><?php if (isset($_SESSION['user_id'])) { echo $_SESSION['first_name']; } ?></a></li>
Upvotes: 1
Views: 2993
Reputation: 377
Like this.
<?php if ($_SESSION['user']['id']):?>
<p>
Hi <?=$_SESSION['user']['name'];?>, you are logged in!
</p>
<?php else:?>
<a href="/user/signin">Sign in</a> or <a href="/user/register">Register</a>
<?php endif;?>
Upvotes: 0
Reputation: 625037
<?php if (isset($_SESSION['user_id']) { ?>
<li><a href="#" title="#"><?php echo $_SESSION['first_name']; } ?></a></li>
<?php } ?>
or with short tags:
<? if (isset($_SESSION['user_id']) { ?>
<li><a href="#" title="#"><?= $_SESSION['first_name']; } ?></a></li>
<? } ?>
You can also use the alternate PHP control structures, which arguably make it more readable:
<?php if (isset($_SESSION['user_id']): ?>
<li><a href="#" title="#"><?php echo $_SESSION['first_name']; } ?></a></li>
<?php endif; ?>
Upvotes: 7
Reputation: 2136
Like this:
<?php if (isset($_SESSION['user_id'])) { echo "<li><a href=\"#\" title=\"#\">"; echo $_SESSION['first_name']; echo "</a></li>"; } ?>
Upvotes: 0
Reputation: 186562
With PHP you determine whether to spit out any content, you don't need to "hide" it per se like CSS...
<?php
if ( isset( $_SESSION['user_id'] ) ) {
?>
<li><a href="#" title="test"><?php echo $_SESSION['user_id'];?></a></li>
<?php
} ?>
Upvotes: 2