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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程