How can variables be preserved when navigating between pages in PHP?

When navigating between pages in PHP, variables can be preserved by using sessions or passing them through URLs. Sessions store variable values across multiple pages for a specific user, while passing variables through URLs involves appending them to the URL as query parameters. Both methods ensure that variables remain accessible as users move between different pages on a website.

// Start a session to store variables
session_start();

// Set a variable in the session
$_SESSION['variable_name'] = 'variable_value';

// Retrieve the variable on another page
$preserved_variable = $_SESSION['variable_name'];