What are the advantages and disadvantages of using sessions to store information in PHP?

Using sessions to store information in PHP can be advantageous because it allows for data to be stored across multiple pages for a specific user during their session. This can be helpful for maintaining user login status, storing shopping cart items, or saving user preferences. However, sessions can also consume server resources and may not be suitable for storing large amounts of data.

// Start a session
session_start();

// Store information in session variables
$_SESSION['username'] = 'JohnDoe';
$_SESSION['cart'] = ['item1', 'item2', 'item3'];

// Retrieve information from session variables
$username = $_SESSION['username'];
$cartItems = $_SESSION['cart'];