What is the correct syntax for setting and retrieving session variables in PHP?

To set and retrieve session variables in PHP, you can use the $_SESSION superglobal array. To set a session variable, you assign a value to a key within the $_SESSION array. To retrieve a session variable, you access the value using the same key within the $_SESSION array. Make sure to start the session with session_start() before setting or retrieving session variables.

// Start the session
session_start();

// Set a session variable
$_SESSION['username'] = 'john_doe';

// Retrieve the session variable
$username = $_SESSION['username'];

// Output the retrieved session variable
echo $username;