如何从 function() 内的 include() 加载变量?
我正在尝试从 .php 文件中获取一些变量。我调用一个函数,其中包含一个包含文件,其中包含我想要的变量,但它不会返回任何内容。在函数内我可以查看变量值,但不能在函数外部查看。
//MODULE INCLUDE
function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
$output = include("../includes/modules/".$folder."/".$row['file']);
return $output;
}
//Load the vars
moduleInclude($mod_ship);
$ship_meth = $MODULE_title_client;
//THE ^^^^ MODULE_title_client never returns anything :(
I'm trying to get some variables from a .php file. I call a function which has an included file in it that has the vars I want but it won't return any. Within the function I can view the variables values, but not outside the function.
//MODULE INCLUDE
function moduleInclude($mid) {
$query = "SELECT file,type FROM sh_module WHERE mid = '{$mid}'";
$exe = mysql_query($query);
$row = mysql_fetch_array($exe);
$folder = moduleFolder($row['type']);
$output = include("../includes/modules/".$folder."/".$row['file']);
return $output;
}
//Load the vars
moduleInclude($mod_ship);
$ship_meth = $MODULE_title_client;
//THE ^^^^ MODULE_title_client never returns anything :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
include 不会返回文件 include 的内容,只是将该文件的代码放在包含该文件的位置。
文件中声明的变量在 include 语句之后可用。但是当您尝试在函数外部访问它们时,变量将无法访问,因为它们现在是函数范围。
您可以更新代码以包含函数外部的该文件。
include does not return the content of file include just put the code of that file at the place where the file is included.
And the variables declared in the files are available after the include statement. but as you are trying to access them outside the function the variable will not be accessible as they are now function scope.
You could update your code to include that file from outside the function.
我认为你可以使用这样的东西:
如果我理解正确的话。它真的很难看,我建议你了解更多关于变量范围使代码变得更好,因为包含在函数中几乎总是错误的方法。我认为我们需要知道包含文件“../includes/modules/”.$folder.”/”.$row['file'] 中发生了什么,以更好地帮助您。
I think you can use something like this:
if I understood you correctly. It is really ugly and I suggest you learn more about variable scopes to make this code better as including in a function is almost always the wrong way to go. I think we need to know what hapens inside your include file "../includes/modules/".$folder."/".$row['file'] to help you better.
只需在包含文件末尾返回一个对象:
然后, include 将返回模块中返回的数组。
Just return an object at the end of your include file:
Then, include will return the array returned in the module.