PHP - Include inside Include(不传递变量)
假设我们有 3 个文件:File1.php、File2.php、File3.php
我想将其放在 File1.php 中包含()File2.php 的位置。然后在 File2.php 中包含() File3.php。我通过在 File3.php 中编写测试文本来测试它,当我查看 File1.php 时,我可以看到该测试文本。
现在我的问题是,如果我在 File3.php 中创建一个变量,说 '$test = "Success!";',然后我尝试在 File1.php 中使用 'echo $test;' 调用它它什么也不输出。它不是传输变量,而是传输标准文本。
这是怎么回事?
(PS 我这样做的原因是因为组织。)
编辑
这是我的确切代码: (还忘了提到我首先获取网站的网址)
file3.php
<?php
$test = "Success!";
file2.php
<?php
include 'http://' . $_SERVER['SERVER_NAME'] . '/file3.php';
file1.php
<?php
include 'http://' . $_SERVER['SERVER_NAME'] . '/file2.php';
echo $test;
Lets say we have 3 files: File1.php, File2.php, File3.php
I want to have it where in File1.php I include() File2.php. Then in File2.php I include() File3.php. I tested it out by writing test text in File3.php, and when I viewed File1.php I could see that test text.
Now my problem is that if I make a variable, say '$test = "Success!";', in File3.php and then I try to call it in File1.php with 'echo $test;' it outputs nothing. It's not transferring the variables, but it is transferring standard text.
What's going on here?
(P.S. The reason I'm doing it like this is because of organization.)
EDIT
Here's my exact code:
(also forgot to mention that I'm grabbing the url of the site first)
file3.php
<?php
$test = "Success!";
file2.php
<?php
include 'http://' . $_SERVER['SERVER_NAME'] . '/file3.php';
file1.php
<?php
include 'http://' . $_SERVER['SERVER_NAME'] . '/file2.php';
echo $test;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能提供更多代码吗?
根据 http://php.net/manual/en/function.include.php:
检查示例 #1
编辑:按照 http://php.net/manual/en/ features.remote-files.php:
因此,您不能使用 URL 包含文件,而是,正如我的评论中所述,使用本地路径。除非您启用了
allow_url_fopen
。这应该有效。Can you provide more code?
Per http://php.net/manual/en/function.include.php :
Check Example #1
Edit: Per http://php.net/manual/en/features.remote-files.php :
So, you cannot include files with the URL, instead, as stated in my comment, use local path. Unless you have
allow_url_fopen
enabled. This should work.如果你的文件有这些行,那么 echo 会打印出你的消息:
File1.php:
File2.php:
File3.php:
If your files have these lines, then echo would print out your message:
File1.php:
File2.php:
File3.php: