How can session variables be effectively used in PHP to store user input across multiple pages?

Session variables can be effectively used in PHP to store user input across multiple pages by starting a session, setting session variables with the user input data, and then accessing these variables on subsequent pages within the same session. This allows the data to persist across different pages as long as the session is active.

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

// Set session variables with user input data
$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];

// Access session variables on subsequent pages
echo 'Welcome, ' . $_SESSION['username'] . '! Your email is: ' . $_SESSION['email'];
?>