How can one avoid the "Undefined variable: PHP_SELF" error in PHP code?

The "Undefined variable: PHP_SELF" error occurs when trying to access the $_SERVER['PHP_SELF'] variable without it being defined. To avoid this error, you can check if the variable is set before using it in your code.

if(isset($_SERVER['PHP_SELF'])) {
    $current_page = $_SERVER['PHP_SELF'];
} else {
    $current_page = '';
}