What are the differences between generating a session variable as a string versus as an array in PHP?
When generating a session variable as a string in PHP, you can store a single value. However, when generating a session variable as an array, you can store multiple values within the same variable. This can be useful when you need to store and retrieve multiple related values in a single session variable. To generate a session variable as an array in PHP, you can use the $_SESSION superglobal array and set the desired key-value pairs within it. This allows you to store multiple values under different keys within the same session variable.
// Generating a session variable as an array
session_start();
// Storing multiple values in the session variable as an array
$_SESSION['user'] = [
'username' => 'john_doe',
'email' => 'john_doe@example.com',
'role' => 'admin'
];
// Retrieving values from the session variable
echo $_SESSION['user']['username']; // Output: john_doe
echo $_SESSION['user']['email']; // Output: john_doe@example.com
echo $_SESSION['user']['role']; // Output: admin