简体   繁体   English

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

[英]PHP can't pass variable to included file

I tried to find a solution for this one: 我试图找到这个解决方案:

file1.php file1.php

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

file2.php file2.php

Hi there! <?php echo $name ?>

Output 产量

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

Need help please :// 需要帮助请://

Your code works fine. 你的代码运行正常。

If you get that error your file1.php certainly does not define $name in the global scope. 如果您收到该错误,则file1.php肯定不会在全局范围内定义$ name。

Fyi, quoted from the docs: Fyi,引自文档:

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. 但是,包含文件中定义的所有函数和类都具有全局范围。

My testcase with php-5.3.3-pl1-gentoo(cli) in case it's a PHP bug in whatever version you are using - but I doubt that: 我的测试用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

Make sure your URL ends in file1.php and not file2.php or some other file that includes file2.php . 确保您的URL以file1.php结尾,而不是file2.php或包含file2.php其他文件。 Also, try reducing both files to exactly what you posted here (plus a <?php at the beginning of file1.php ) to rule out surrounding code. 另外,尝试(加了两个文件还原到你贴什么位置<?php之初file1.php )排除周围的代码。

I had the same problem and fixed it like this: 我有同样的问题,并修复如下:

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

file2.php file2.php

$name = $name; //add this line
Hi there! <?php echo $name ?>

I can't explain why it should work like this. 我无法解释为什么它应该像这样工作。 Just it worked. 只是它奏效了。

I sometimes get cuzzled (short for confuzzled) and forget to use the base file for checking a project online. 我有时会被愚弄(为了被困的短暂)而忘记使用基本文件在线检查项目。

For instance, if using a base file called index.php with an include inc.php, make sure to launch index.php and not the include file. 例如,如果使用名为index.php且包含inc.php的基本文件,请确保启动index.php而不是包含文件。

The inc.php file will show errors in a browser because it's not linked to the index.php variables, whereas the index.php file contains both the variables and includes. inc.php文件将在浏览器中显示错误,因为它没有链接到index.php变量,而index.php文件包含变量和包含。

I use an environment variable instead. 我改用环境变量。 It gets around this problem. 它解决了这个问题。

Ie

$_ENV["name"] = "Jacob";

and inside file2.php file2.php里面

echo $_ENV["name"];

如果你想打印/使用$name变量,那么你必须首先在file2声明该变量,就像$name="" ,然后你可以轻松地使用它,而不需要声明,你将得到Undefined Variable错误。

Try to use this code 尝试使用此代码

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

** and inside file2.php **和file2.php里面

global $name;
echo "Hi there! ".$name;

I think that will work 我认为这会奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM