How can sessions be used in PHP to store and retrieve values?

Sessions in PHP can be used to store and retrieve values across multiple pages for a particular user session. To store a value in a session, you can use the $_SESSION superglobal array and assign a value to a specific key. To retrieve the stored value, you can simply access the key in the $_SESSION array. Make sure to start the session using session_start() at the beginning of your PHP script.

<?php
// Start the session
session_start();

// Store a value in the session
$_SESSION['username'] = 'JohnDoe';

// Retrieve the stored value
$username = $_SESSION['username'];

// Output the retrieved value
echo $username;
?>