'Include multiple files with 1 line
Is it possible to include multiple files by using 1 line in the php file?
So lets say my main file is main.php and I want to include ic1.php, ic2.php and ic3.php.
I tried creating a new file include_all.php, placed a list with all includes in this file and included this file in main.php. But apparently you cant include included includes.
Solution 1:[1]
Try creating array of file names and then looping through array, like:
$fileList = array(
'ic11.php',
'ic2.php',
'ic3.php'
)
$dirPath = '';
foreach($fileList as $fileName)
{
include_once($dirPath.$fileName);
}
If your files are inside some directory then set proper path in $dirPath variable.
Solution 2:[2]
using functions WILL NOT WORKS correctly
such like
function include_multiple() {
foreach ( func_get_args() as $arg ) {
include_once($arg);
}
}
or
array_map( function($f){include_once($f.'.php');}, ['lang','func','db'] );
because of "variable scope"
your variables in second file will loaded into the function,
so variables will not accessible outside of include_multiple function
I think this is best way :
foreach(['func','db','lang'] as $f) include_once($f.'.php');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sid |
| Solution 2 |
