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

PHP sessions can be effectively used to store and retrieve values by starting a session, assigning values to session variables, and accessing these values across different pages within the same session. To store a value in a session variable, you can use the $_SESSION superglobal array, and to retrieve the stored value, you can simply access the session variable using the same array.

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

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

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

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