What are the potential pitfalls of not properly initializing a PHP session before accessing session variables?

Not properly initializing a PHP session before accessing session variables can lead to errors or unexpected behavior in your application. To solve this issue, you should always start the session using session_start() before trying to access or set any session variables.

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

// Access session variables
$_SESSION['username'] = 'JohnDoe';
echo $_SESSION['username'];
?>