在文件B中包含文件A,反之亦然

What are the differences in variable and function scoping, between the "includer" and the "includee"?

For example, these two tests work identically, but are there scoping subtleties I should know about?

Test 1:

File "one.php":

<?php 
$a = 5;
include("two.php");
?>

File "two.php:

<?php
function f($x) { return $x * 2; }
echo f($a);
?>

Test 2:

File "one.php":

<?php 
$a = 5;
?>

File "two.php:

<?php
include("one.php");

function f($x) { return $x * 2; }

echo f($a);
?>

When you execute a PHP file, it starts off in the global scope. The include documentation states;

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Since when you include the second file you're in both cases in global scope, the variable scope will stay global and everything else included will always have global scope. In other words, everything in both files and in both cases ends up in global scope and there is no difference in scoping between the two.

When you are including a file you can think of it like a single file ie:

Test 1 as a single file:

<?php 
  // execute commands in this file
  $a = 5;


  // then continue execution of include("two.php");
  function f($x) { return $x * 2; }
  echo f($a);
?>

Test 2 as a single file:

<?php 
  // first include include("one.php");
  $a = 5;

  // then continue execution of two.php
  function f($x) { return $x * 2; }
  echo f($a);
?>

Hope you can understand the difference between the two. In essence it is similar to copying and then pasting the included commands of the include file.

Variables and function scope is as if it were the same file as long as the included file precedes the variable or function call in the "includee"