What potential issues can arise when setting a session variable within an if statement in PHP?

Setting a session variable within an if statement in PHP can lead to unexpected behavior if the condition is not met. To avoid this issue, it's recommended to set the session variable outside of the if statement or ensure that the if statement always evaluates to true when setting the session variable.

<?php
session_start();

// Check if condition is met before setting session variable
if ($condition) {
    $_SESSION['variable'] = 'value';
}

// Alternatively, set the session variable outside of the if statement
$_SESSION['variable'] = ($condition) ? 'value' : $_SESSION['variable'];
?>