Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php定义的函数不';不包括在内的转移_Php_Php Include - Fatal编程技术网

Php定义的函数不';不包括在内的转移

Php定义的函数不';不包括在内的转移,php,php-include,Php,Php Include,所以,我有一个名为init.php的文件,应该放在我网站上每个页面的顶部。在本例中,它位于my index.php中。在init.php中有一段代码,包含名为include的文件夹中的所有文件。当我试图调用index.php文件中的一个函数时,该函数是在一个应该包含的文件中定义的,我只得到一个错误,即调用未定义的函数errors\u return()。知道这里怎么了吗 //index.php <?php include "php\init.php"; //errors_return();

所以,我有一个名为init.php的文件,应该放在我网站上每个页面的顶部。在本例中,它位于my index.php中。在init.php中有一段代码,包含名为include的文件夹中的所有文件。当我试图调用index.php文件中的一个函数时,该函数是在一个应该包含的文件中定义的,我只得到一个错误,即
调用未定义的函数errors\u return()
。知道这里怎么了吗

//index.php
<?php
include "php\init.php";
//errors_return(); is a defined function in functions.php
?>

//init.php
<?php
//error_reporting(0);
foreach (glob("include\*.php") as $filename) {
     include $filename;
}
$GLOBALS["errors_log"] = array();
session_start();
?>

//sample of functions.php
<?php
function error($msg = "default error"){
    array_push($GLOBALS["errors_log"],$msg);
}
function errors_return(){
    if(!empty($GLOBALS["errors_log"])){
        foreach($GLOBALS["errors_log"] as $e){
            echo '<p style="position:relative;">'.$e.'</p>';
        }
    }
    else {
        return null;
    }
}
?>

folder structure
root (folder)
   index.php
   php (folder)
      init.php
      include (folder)
          functions.php
//index.php
//init.php
//functions.php示例
文件夹结构
根目录(文件夹)
index.php
php(文件夹)
init.php
包括(文件夹)
functions.php

您在代码的双引号内使用了反斜杠
\
。PHP中的反斜杠告诉解释器一个转义序列开始,反斜杠后面的字符将被解释为这样的转义序列


作为解决办法,尝试用双反斜杠替换反斜杠
\\
,双反斜杠将转义反斜杠本身并将其解释为普通字符,或者只使用斜杠
/
而不是反斜杠。

您所要做的就是将目录分隔符从正斜杠
/
更改为反斜杠
\
。我已经在我的机器上测试过了,它已经运行了。

所以我发现了问题所在。似乎当include发生在
foreach
循环中时,它不在文件本身所包含的正确范围内。但我不知道如何解决这个问题

foreach (glob("include\*.php") as $filename) {
     include $filename; //not included with the file
}
include "sample/dir/sample.php"; //included the with file

使用
/
作为目录分隔符也尝试了这一点,但它没有做任何事情。
glob
是否返回您期望的结果?尝试require并查看是否有任何错误直到对未定义的函数errors发出相同的
调用\u return()
它仍然会调用未定义的函数错误,但仅在index.php页面上。如果我通过浏览器访问init.php并让它调用一个函数,出于某种原因,这是可行的。但是,当它包含在另一个页面中时,应该定义的函数将不起作用。您是否尝试用require替换init.php中的
include
?我认为那里的循环可能有路径问题。