dthjnc306679 2012-02-20 18:38
浏览 27
已采纳

在文件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);
?>
  • 写回答

2条回答 默认 最新

  • dougourang1856 2012-02-20 18:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探