1

I am trying to reference a session variable in this line of code

<?php
     if (!isset($_SESSION['name'])) {
               echo '<a class="nav-link" href="URL">Sign Up</a>';
     } else{
               echo '<a class="nav-link" href="URL">$_SESSION["name"]</a>';
     };
?>

However can't seem to find a way for this to actually pull from the session data as it is being treated as a string and outputting literally what is there $_SESSION["name"] Is there a way around this?

thanks!

1
  • do a print_r($_SESSION) and let us know the results Commented Apr 1, 2020 at 18:17

1 Answer 1

1

Try this: '.' is used for concatenation in php.

$_SESSION['name'] = 'Ali';
echo '<a class="nav-link" href="URL">'. $_SESSION["name"] . '</a>';

This output the session['name'], which is 'Ali'.

As a result, your code will be:

<?php
 if (!isset($_SESSION['name'])) {
           echo '<a class="nav-link" href="URL">Sign Up</a>';
 } else{
           echo '<a class="nav-link" href="URL">'. $_SESSION["name"] . '</a>';
 };
?>

Good Luck

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

Comments

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.