-3

I am trying to display the logout button only if any user is logged in.

following is the code:

<?php
     if(isset($_SESSION['user']))  
     { echo '<li><a href='logout.php'>Logout</li>'; }
?>

But it is giving error. Error:Parse error: syntax error, unexpected 'logout' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\index.php on line 42 Help me to display this link.

Disha
  • 17
  • 4

2 Answers2

0

You can't use unescaped ' characters in string literals delimited by ' characters. Either:

  • Use " instead
  • Escape the '
  • Drop out of PHP mode and just write the HTML as normal template code

Such:

<?php
     if(isset($_SESSION['user'])) {
?>
          <li><a href='logout.php'>Logout</li>
<?php
     }
?>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You are closing your quotes and opening them again. Try this:

<?php
     if(isset($_SESSION['user'])) {
         echo '<li><a href="logout.php">Logout</li>';
     }
?>

Note the double quotes.

Alternatively, escape the quotes like this:

<?php
     if(isset($_SESSION['user'])) {
         echo '<li><a href=\'logout.php\'>Logout</li>';
     }
?>
rjdown
  • 9,162
  • 3
  • 32
  • 45