gpt4 book ai didi

PHP 无法将变量传递给包含的文件

转载 作者:可可西里 更新时间:2023-10-31 23:55:21 25 4
gpt4 key购买 nike

我试图找到解决这个问题的方法:

file1.php

$name = "Jacob";
include ("file2.php");

file2.php

Hi there! <?php echo $name ?>

输出

Hi There! Notice: Undefined variable: name in /volume1/web/test/file2.php on line 9 

需要帮助请://

最佳答案

您的代码工作正常。

如果你遇到这个错误,你的 file1.php 肯定没有在全局范围内定义 $name。

仅供引用,引用自文档:

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.

我使用 php-5.3.3-pl1-gentoo(cli) 的测试用例,以防它是您使用的任何版本中的 PHP 错误 - 但我怀疑:

[thiefmaster@hades:~]> cat inc1.php
<?php
$name = 'bleh';
include('inc2.php');
?>
[thiefmaster@hades:~]> cat inc2.php
Hi there! <?php echo $name; ?>
[thiefmaster@hades:~]> php inc1.php
Hi there! bleh

关于PHP 无法将变量传递给包含的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4356405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com