So, I have a file called init.php that is supposed to go on top of every page on my website. In this case it is in my index.php. Inside that init.php is a piece of code that includes all files in a folder called include. When I try to call a function within the index.php file that is defined in a file that is supposed to be included I just get an error saying Call to undefined function errors_return()
. Any idea what's wrong here?
//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
So I found out what the problem here is. It seems that when the include happens in the foreach
loop, It's not in the right scope to be included with the file itself. I have no idea how to fix this though.
foreach (glob("include\*.php") as $filename) {
include $filename; //not included with the file
}
include "sample/dir/sample.php"; //included the with file