How can including files in PHP affect the scope of variables like $GLOBALS and lead to unexpected behavior?

Including files in PHP can affect the scope of variables like $GLOBALS by introducing variable collisions or overwriting existing variables. This can lead to unexpected behavior as variables may be unintentionally modified or accessed in different parts of the code. To solve this issue, it's important to use include_once or require_once to ensure that files are only included once to prevent variable redeclarations.

<?php
include_once 'file1.php';
include_once 'file2.php';
// Code that uses variables from file1.php and file2.php
?>