What are the differences between using cookies and sessions for storing variables in PHP?

Cookies and sessions are both used to store variables in PHP, but there are some key differences between the two. Cookies store data on the client's browser, while sessions store data on the server. Cookies have a limited storage capacity and can be manipulated by the client, while sessions are more secure and can store larger amounts of data. Sessions also expire when the user closes their browser, while cookies can have an expiration date set.

// Using sessions to store variables
session_start();
$_SESSION['username'] = 'john_doe';
$_SESSION['email'] = 'john.doe@example.com';

// Retrieving variables from session
$username = $_SESSION['username'];
$email = $_SESSION['email'];