·  PHP – Terms – Session – Variable – Delete

 

- Info:

  - This tutorial shows how to delete session variable using unset() function on superglobal array $_SESSION like this:

    unset($_SESSION['username']);

 

- Example:

  - In this example session variable 'username' is created first time you call a web page.

  - After you refresh the page session variable 'username' will be taken from session and displayed.

  - Create PHP Console Application Test.php.

 

    Test.html

    <?php

   

      session_start();

      

      unset($_SESSION['username']);

   

      if (array_key_exists('username', $_SESSION)) {    

        echo("Username = " . $_SESSION['username']);    

      }

      else {

        echo("No username. Setting one.");

        $_SESSION['username'] = "jason"; 

      }

   

    ?>

 

    output1

    No username. Setting one.

 

    output2

    Username = jason