How can variables retain their values in PHP scripts when a new value is assigned?

Variables can retain their values in PHP scripts when a new value is assigned by using session variables. Session variables store data across multiple pages within a user's session. By starting a session and assigning values to session variables, the values can be accessed and retained throughout the user's interaction with the website.

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

// Assign a value to a session variable
$_SESSION['variable_name'] = 'value';

// Retrieve the value from the session variable
echo $_SESSION['variable_name']; // Output: value
?>