PHP - Include inside Include(不传递变量)

发布于 2024-12-07 05:20:34 字数 806 浏览 0 评论 0原文

假设我们有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-12-14 05:20:34
echo "$test"; //with double quotes :)

// or echo $test;

您能提供更多代码吗?

根据 http://php.net/manual/en/function.include.php

当包含一个文件时,它包含的代码继承包含发生的行的变量范围。从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域。

检查示例 #1

编辑:按照 http://php.net/manual/en/ features.remote-files.php

此外,URL 可以与 include()、include_once()、require() 和 require_once() 语句一起使用(从 PHP 5.2.0 开始,必须为这些语句启用allow_url_include)

因此,您不能使用 URL 包含文件,而是,正如我的评论中所述,使用本地路径。除非您启用了 allow_url_fopen。这应该有效。

echo "$test"; //with double quotes :)

// or echo $test;

Can you provide more code?

Per http://php.net/manual/en/function.include.php :

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.

Check Example #1

Edit: Per http://php.net/manual/en/features.remote-files.php :

In addition, URLs can be used with the include(), include_once(), require() and require_once() statements (since PHP 5.2.0, allow_url_include must be enabled for these)

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.

妄想挽回 2024-12-14 05:20:34

如果你的文件有这些行,那么 echo 会打印出你的消息:

File1.php:

<?php
    include 'File2.php';
    echo $test;
?>

File2.php:

<?php
    include 'File3.php';
?>

File3.php:

<?php
    $test='success!';
?>

If your files have these lines, then echo would print out your message:

File1.php:

<?php
    include 'File2.php';
    echo $test;
?>

File2.php:

<?php
    include 'File3.php';
?>

File3.php:

<?php
    $test='success!';
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文