What are best practices for debugging PHP code, especially when dealing with session-related problems?

When debugging PHP code, especially when dealing with session-related problems, it's important to check if sessions are being properly started and managed. Make sure session_start() is called before any output is sent to the browser, and session variables are being set and accessed correctly. Additionally, check for any errors in the session configuration in php.ini.

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

// Set a session variable
$_SESSION['user_id'] = 123;

// Access the session variable
$user_id = $_SESSION['user_id'];

// Check if session variable exists
if(isset($_SESSION['user_id'])) {
    echo "User ID: " . $_SESSION['user_id'];
} else {
    echo "Session variable user_id not set";
}
?>