How can including files affect the scope of variables in PHP?

Including files in PHP can affect the scope of variables because when a file is included, all the code within that file is executed in the same scope as the line where the include statement is written. This means that any variables declared within the included file will be available in the parent file, potentially causing naming conflicts or unintended variable overwriting. To avoid this issue, it is recommended to use functions or classes to encapsulate variables and prevent them from leaking into the global scope.

// parent.php
include 'child.php';

// child.php
function myFunction() {
    $variable = 'Hello World';
    echo $variable;
}

myFunction();