How can the use of $_SESSION variables prevent issues with session side-effects in PHP?

When using $_SESSION variables in PHP, it's important to prevent session side-effects by starting the session at the beginning of the script and using session_start() function. This ensures that the session is properly initialized and prevents any issues with session variables not being available throughout the script.

<?php
session_start();

// Now you can safely use $_SESSION variables throughout your script
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'john_doe';
?>