What role do sessions play in controlling the execution of PHP functions?

Sessions play a crucial role in controlling the execution of PHP functions by allowing the server to store user-specific data across multiple pages. This enables the server to keep track of user information, such as login status or shopping cart contents, throughout the user's session on the website.

// Start a new session or resume the existing session
session_start();

// Store data in the session
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'example_user';

// Retrieve data from the session
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];

// Destroy the session when the user logs out or the session expires
session_destroy();