在回声中取消设置会话导致问题

I am getting the following error and can not figure out why. I've been staring at this for too long..

Error:

Parse error: syntax error, unexpected T_UNSET in blah/blah/blah

My code:

Essentially, I am trying to give the user the ability to clear a session via clicking a link. Not sure where I am going wrong within my syntax... Any help would be much appreciated!

NOTE: Yes, my code is within php blocks

echo "<span><a href='" . unset($_SESSION['vertical']) . "'>clear "
 . $vertical . "</a></span>";

Thanks in advance!

You cannot call PHP functions after the page has finished loading. PHP is a server-side technology and works on the server, not on the client's computer. Which means, you won't be able to call a PHP function without sending the details to the script.

If you're trying to trigger the unset function when the user clicks the link, then you could create a link to a script where you unset the $_SESSION variable:

<span><a href='somepage.php?somevar=42'>foo</a></span>

When the user clicks on the link, they'll be taken to somepage.php. Now, you can check if the somevar key is set and then unset the session in the script:

<?php
session_start();

if (isset($_GET['somevar'])) {
    unset($_SESSION['vertical']);
}

If you want to do this without a page refresh, then you might want to take a look at AJAX.

When you write unset($_SESSION['x']) x in session will be deleted. It will executed immediately. You should give a link to a page that will do the unsetting.

If you want to clear a session variable in same page you need to make an AJAX call to another page which has the code unset($_SESSION['x']).

You should do like this:

 <a href="sessiondestroy.php">Clear " . $vertical . "</a>

 and in clearsession.php write:

  <?php

  session_start();

  unset($_SESSION['vertical']);

  ?>

 Let me know what happened?