0

i want to display Usernames after login or signup in the Navbar using the function links () but the variable holding the username wont show, only displays "$acn";

 function links()
{
if(!isset($_SESSION['username']))
     {
         echo '         <div class="collapse navbar-collapse" id="signup">
            <!--<form class="navbar-form navbar-right" role="search">
              <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
              </div>
              <button type="submit" class="btn btn-default">Submit</button>
            </form>-->
          <ul class="nav navbar-nav navbar-right" id="signup">
            <li><a href="signup.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
            <li><a href="index.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
          </ul>
        </div>';
         }
         else
         {
           $acn= $_SESSION['username'];
        echo   '<div class="collapse navbar-collapse" id="signup">
          <ul class="nav navbar-nav navbar-right" id="signup">
            <li><a href=\"account.php\"> $acn </a></li>
            <li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> Log Out</a></li>
          </ul>
        </div>';
    }
}

and the function appears like this

<nav class="navbar navbar-inverse" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">OOOO</a>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#signup">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <?php echo links(); ?>
    </div>
</nav>

How do i achieve this please, thanks.

2 Answers 2

1

In PHP, if you have a string wrapped in single quotes variables will not be parsed.

'$acn' is just a string with the value $acn. "$acn" will be parsed to be the value of the $acn variable.

Please also note that you are echoing the output of the links() function, but that function has no return value.

Sign up to request clarification or add additional context in comments.

Comments

0

While your use of $acn would work fine in double quotesit does not behave in the same manor when contained inside single quotes.

If you wish to use single quotes you must concatenate the string:

  <li><a href=\"account.php\"> '.$acn.' </a></li>

Other things to note with your code, while using single quotes you will not need to escape double quotes and also there is an echo inside the links function so its not required here: <?php echo links(); ?>

1 Comment

This is not the cause of the problem the user is describing, though you are correct that the echo is unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.