PHP Terms Session Variable Create Scalar

 

- Info:

- This tutorial shows how to store Scalar into session and retreive it by using superglobal array $_SESSION like this:

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

 

- Example:

- In this example session variable 'username' of datatype String 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