0
<?php
    if(!isset($_SESSION['user_name'])){
        echo '<a href="login.php" role="button" aria-expanded="false"> LogIn <span class="label">LogIn to System</span></a>';
    elseif(isset($_SESSION['user_name'])){
        echo '<a href="login.php" role="button" aria-expanded="false">' . $_SESSION['user_name'] . '<span class="label">it is you</span></a>';
    }
?>

It's a bit of simple question. Can someone help me with debugging this part of code? Then I introduce it in my php file, the page is blank. No content is displayed. It's just white page, without any content. After removing this, I can see the content without problem.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
InDeepTerror
  • 29
  • 1
  • 6
  • 1
    blank page = something blew up and you've got all debug options turned off. turn on `error_reporting` and `display_errors` and try again. in your case, syntax error missing `}` before `elseif` – Marc B Jun 18 '15 at 14:15
  • 2
    You missed a `}` right here: `elseif` – Daan Jun 18 '15 at 14:15

2 Answers2

0

change:

elseif(isset($_SESSION['user_name'])){

to:

}elseif(isset($_SESSION['user_name'])){

See the "}"

PHPhil
  • 1,555
  • 13
  • 27
0

As others have stated, you are missing a closing curly bracket (}).

Here's my suggestion, loose the brackets if you have only one instruction per if-statement, as such:

<?php
if(!isset($_SESSION['user_name']))
    echo '<a href="login.php" role="button" aria-expanded="false"> LogIn <span class="label">LogIn to System</span></a>';
elseif(isset($_SESSION['user_name']))
    echo '<a href="login.php" role="button" aria-expanded="false">' . $_SESSION['user_name'] . '<span class="label">it is you</span></a>';

Of course, you will have to put the brackets back if you plan on adding other instructions (eg. lines) to your statements.

Your code could also be shortened by:

<?php
if(isset($_SESSION['user_name']))
    echo '<a href="login.php" role="button" aria-expanded="false">' . $_SESSION['user_name'] . '<span class="label">it is you</span></a>';
else
    echo '<a href="login.php" role="button" aria-expanded="false"> LogIn <span class="label">LogIn to System</span></a>';

Finally, you can also use ternary expression:

<?php
echo '<a href="login.php" role="button" aria-expanded="false">';
echo isset($_SESSION['user_name']) ? $_SESSION['user_name'].'<span class="label">it is you</span>' : 'LogIn<span class="label">LogIn to System</span>';
echo '</a>';
Community
  • 1
  • 1
D4V1D
  • 5,805
  • 3
  • 30
  • 65