What role do sessions play in PHP scripts and how can they impact the execution of code?

Sessions in PHP scripts play a crucial role in maintaining user data across multiple pages or requests. They allow for the storage and retrieval of user-specific information, such as login credentials or shopping cart items. Sessions can impact the execution of code by providing a way to track user interactions and personalize their experience on a website.

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

// Store user data in the session
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';

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

// Use the user data in the code
echo "Welcome back, $username! Your email is $email.";
?>