PHP变量没有通过include/require传递给函数


PHP variables not being passed to functions with include/require

我试图使一个简单的函数包含文件,但由于某种原因,变量没有被传递给。

在test.php文件中:

<?php
function parse($template) {
    require $template;
}
$test = "test";
parse("stuff.php");
?>

文件stuff.php:

<?php
echo $test;
?>

我将得到错误:

Notice: Undefined variable: test in /opt/lampp/htdocs/testinggrounds/stuff.php on line 2

但是,如果我在test.php

中替换此语句
parse("stuff.php");

require "stuff.php";

它会工作得很好

当她存在的时候为什么要做一个函数?

你只需要调用require 'stuff.php';,就这样了