What are the potential pitfalls of setting or reading a session variable before session_start in PHP?

Setting or reading a session variable before session_start in PHP can lead to unexpected behavior or errors because session_start initializes the session and headers, making the session data available. To avoid this issue, always call session_start before setting or reading session variables.

<?php
// Always call session_start before setting or reading session variables
session_start();

// Now it's safe to set or read session variables
$_SESSION['user_id'] = 123;
$user_id = $_SESSION['user_id'];
?>