简体   繁体   English

如何将php变量传递给.php include?

[英]How do I pass a php variable to a .php include?

I have a file, lets say it's index.php where the very beginning of the file has an include for "include.php". 我有一个文件,可以说是index.php,其中文件的开头有一个包含“ include.php”的内容。 In include.php I set a variable like this: 在include.php中,我设置了这样的变量:

<?php $variable = "the value"; ?>

then further down the in index.php I have another include, say "include2.php" that is included like this: 然后进一步在index.php中,我有另一个include,例如这样包含的“ include2.php”:

<?php include(get_template_directory_uri() . '/include2.php'); ?>

How can I call the "$variable" that I set in the first include, in "include2.php"? 如何在“ include2.php”中调用在第一个include中设置的“ $ variable”?

The exact code that I am using is as follows: 我正在使用的确切代码如下:

The very first line of the index.php I have this line index.php的第一行我有这行

<?php include('switcher.php'); ?>

Inside switcher.php I have this 在switcher.php内部,我有这个

<?php $GLOBALS["demo_color"] = "#fffffe"; ?>

If I use this in index.php, it works 如果我在index.php中使用它,它将起作用

<?php echo $GLOBALS["demo_color"]; ?>

However, If I use the following code to include another php file 但是,如果我使用以下代码来包含另一个php文件

<?php include(get_template_directory_uri() . '/demo_color.php'); ?>

then inside demo_color.php I have this code: 然后在demo_color.php中,我有以下代码:

<?php echo "demo color:" . $GLOBALS["demo_color"]; ?>

The only thing it outputs is "demo color:" 它输出的唯一内容是“演示颜色”:

edited for code-formatting 编辑用于代码格式

It simply can be used in include2.php, unless the inclusion of include.php happens inside of a different scope (ie inside a function call). 可以简单地用在include2.php中,除非include.php的包含发生在另一个作用域之内(即函数调用之内)。 see here . 看这里

If you want to be completely explicit about the intention of using the variable across the app, use the $GLOBALS["variable"] version of it's name, which will always point to the variable called variable in the global scope. 如果要完全清楚地在应用程序中使用变量的意图,请使用其名称的$ GLOBALS [“ variable”]版本,该版本将始终指向全局范围内称为variable

EDIT : I conducted a test against php 5.3.10 to reconstruct this: 编辑 :我对php 5.3.10进行了测试,以重建此:

// index.php
<?php
include("define.php");
include("use.php");

// define.php
$foo = "bar";

// use.php
var_dump($foo);

This works exactly as expected, outputting string(3) "bar". 这完全按预期工作,输出string(3)“ bar”。

<?PHP
//index.php
$txt='hello world';
include('include.php');

<?PHP
//include.php
echo $txt; //will output hello world

So it does work. 这样就可以了。 Though there seems to be a bigger issue since this is likely to be difficult to maintain in the future. 尽管似乎存在一个更大的问题,因为将来可能很难维护。 Just putting a variable into global namespace and using it in different files is not a best practice. 仅将变量放入全局名称空间并在不同文件中使用它不是最佳实践。

To make the code more maintainable it might be an idea to use classes so you can attach the variables you need explicit instead of just using them. 为了使代码更具可维护性,使用类可能是一个主意,因此您可以附加需要的变量而不是仅仅使用它们。 Because the code around is not showed it is not clear what is your exact need further but it will be likely the code can be put in classes, functions etc. If it is a template you could think about an explicit set() function to send the variable data to the templates and extract() it there. 由于未显示周围的代码,因此尚不清楚您的确切需求是什么,但是可能会将代码放入类,函数等中。如果是模板,则可以考虑发送一个显式的set()函数。将变量数据发送到模板,然后在其中提取()。

edit: 编辑:

In addition based on the information first set your error_reporting to E_ALL and set the display_errors to 1. So you get all errors since the information you placed in your updated question gives indications that a missing variable is used as a constant which should raise errors all over the place. 另外,根据信息,首先将error_reporting设置为E_ALL并将display_errors设置为1。因此,您将得到所有错误,因为您放置在已更新问题中的信息表明,缺少变量用作常量会引起错误。这个地方。

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

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