Php 从函数中检索include中的变量
为什么这样不行?不太明白:Php 从函数中检索include中的变量,php,include,Php,Include,为什么这样不行?不太明白: function inc($page){ include(get_template_directory().'/inc/'.$page.'.php'); } function getPagebyID($id){ $post=get_post($id); inc('page_template'); } [其中“/inc/page_template.php”内容包含:] 在函数中使用include时,即使使用“global”,变量也不再在inc
function inc($page){
include(get_template_directory().'/inc/'.$page.'.php');
}
function getPagebyID($id){
$post=get_post($id);
inc('page_template');
}
[其中“/inc/page_template.php”内容包含:]
在函数中使用include时,即使使用“global”,变量也不再在include文件中工作。如果您直接包含而不调用函数,那么我认为它是有效的
我还尝试将
$post
作为参数传递给函数inc()
,但这似乎也不起作用。函数可以很好地处理包括:
问题是echo$post->title
或$post=get\u post($id)代码>
使用:
$post = "ID: " . $id;
及
工作正常,因此问题在这两个变量中的任何一个内。生成变量的代码在另一个调用函数“inc”的函数内。……这可能是问题所在吗?如果代码与上述代码类似,那么只要变量返回有效值,它就可以工作。如果代码是不同的,那么你应该发布它,否则任何人都可以猜测问题可能是什么。
$post = "ID: " . $id;
echo $post;