如何在另一个文件的函数中使用一个 PHP 文件中的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4001915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use variables from one PHP file in another file's function?
提问by PEPPERONI
NOTE: I have read about 25 questions thus far that are peripherally related to this, but never answer the question directly of HOW TO USE THOSE VARIABLES CORRECTLY. I apologize if it sounds like a duplicate.
注意:到目前为止,我已经阅读了大约 25 个与此相关的问题,但从未直接回答如何正确使用这些变量的问题。如果听起来像重复,我深表歉意。
I am rewriting a flat e-commerce site (all hand-made.htm
files!) so that some of the pages are generated using PHP scripts. This is all just to make my life easier for now. I have told my boss repeatedly that this site needs to be nuked from orbit, and there is a full-blown CMS in the pipeline. In the meantime this site NEEDS to be revamped. Site in question: http://www.leather.com
我正在重写一个平面电子商务网站(都是手工制作的.htm
文件!),以便使用 PHP 脚本生成一些页面。这一切只是为了让我现在的生活更轻松。我一再告诉我的老板,这个网站需要从轨道上核爆,并且有一个成熟的 CMS 正在筹备中。与此同时,这个网站需要改造。相关网站:http: //www.leather.com
I have been using a constructor_page.php
that declares and initializes a few variables such as $itemName
and $imagePath
that are used in include()
'd content pages (trimmed example follows):
我一直在使用constructor_page.php
该声明并初始化几个变量,如$itemName
和$imagePath
那些在使用include()
“d内容页面(例如修剪如下):
<?php
$imagePath = 'http://leather.com/images/harley_leather_boots/d81024_ladies_harley_leather_boots_360.jpg';
$itemName = 'Faded Glory Harley Boots';
include 'library/content.php'; //this in turn includes more files like the nav bar
?>
This way I just save constructor_page.php
as the item page in question, modify a few variables, and the include()
statements take care of the rest. This part works okay.
这样我只需保存constructor_page.php
为有问题的项目页面,修改一些变量,include()
其余的由语句处理。这部分工作正常。
My issue is with generating "department" pages automatically from the files in the same directory. I would like the index.php to grab those same variables in a loop:
我的问题是从同一目录中的文件自动生成“部门”页面。我希望 index.php 在循环中抓取这些相同的变量:
<?php
foreach (glob("*.php") as $filename) {
include $filename;
echo '<div class="related">';
echo '<a href="';
echo $filename;
echo '">';
echo '<img src="';
echo $imagePath;
echo '" alt="';
echo $imageAlternateText;
echo '" class="frame-related align-left-related"';
echo '></a>';
echo '<a href="';
echo $filename;
echo '">';
echo $itemName;
echo '</a>';
echo '</div>';
}
?>
This is intended to, for each php file (yes I know it'll recurse to itself), grab the relevant variables and create a nice picture/link inside a that floats left, in essence generating the department page thumbnails automatically based on whatever php files it finds in there.
这是为了,对于每个 php 文件(是的,我知道它会递归到自己),获取相关变量并在向左浮动的 a 中创建一个漂亮的图片/链接,本质上是基于任何 php 自动生成部门页面缩略图它在那里找到的文件。
Yes, it's horrible. Yes, it's hackish. Needless to say, it does not work due to the scope of the variables. I am open to suggestion here, as all I need is for the department page to generate little pictures and links based on what it finds in the folder SOME HOW. Thanks in advance!
是的,这太可怕了。是的,这很hackish。不用说,由于变量的范围,它不起作用。我对这里的建议持开放态度,因为我所需要的只是让部门页面根据它在文件夹中找到的内容生成小图片和链接。提前致谢!
回答by mellowsoon
You must have some other problem. Let me demonstrate:
你肯定有其他问题。让我演示一下:
<?php // This is index.php
$foo = 'bar';
include('page2.php');
?>
<?php // This is page2.php
include('page3.php');
?>
<?php // This is page3.php
echo $foo;
?>
If you go to index.php, it will display "bar". Included PHP scripts inherit the scope where they were included. There's no need to use a "global" statement at any time. If however your page3.php script looked like this:
如果你去 index.php,它会显示“bar”。包含的 PHP 脚本继承了它们被包含的范围。任何时候都不需要使用“全局”语句。但是,如果您的 page3.php 脚本如下所示:
<?php // This is page3.php
function display() {
echo $foo;
}
display();
?>
That wouldn't display anything, because now $foo is in the scope of the display() function, so it would need to be rewritten like this:
那不会显示任何东西,因为现在 $foo 在 display() 函数的范围内,所以它需要像这样重写:
<?php // This is page3.php
function display() {
global $foo;
echo $foo;
}
display();
?>
So you must be doing something else wrong if your variables aren't available in other scripts.
因此,如果您的变量在其他脚本中不可用,那么您一定是在做其他错误。
回答by VoteyDisciple
Simply declare
简单地声明
global $imagePath;
(And likewise for other variables you want to use in more than one script or function.)
(对于要在多个脚本或函数中使用的其他变量也是如此。)
回答by cointilt
Like VoteyDIsciple said, you have to declare it at the top of the page first as being a global variable like so:
就像 VoteyDIsciple 所说的那样,您必须首先在页面顶部将其声明为全局变量,如下所示:
global $imagePath;
$imagePath = 'path/to/the/image.jpg';
Then on any page you want to use the global variable you must reference it as being global first before using it.
然后在任何要使用全局变量的页面上,您必须在使用它之前首先将其引用为全局变量。
So on the page you are using echo $imagePath; you must first do this again.
所以在你使用的页面上 echo $imagePath; 您必须首先再次执行此操作。
global $imagePath;
Global variables get pretty dangerous so I would try looking into a more MVC type of framework where your variables you create in your Controllers get passed into your view files. http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
全局变量变得非常危险,所以我会尝试研究更多 MVC 类型的框架,其中您在控制器中创建的变量被传递到您的视图文件中。http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
A good MVC Framework I enjoy developing in is Codeigniter http://codeigniter.com
我喜欢开发的一个很好的 MVC 框架是 Codeigniter http://codeigniter.com