3

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.

Blue
  • 22,608
  • 7
  • 62
  • 92
Johnenzone
  • 39
  • 1
  • 4

2 Answers2

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');
a55
  • 376
  • 3
  • 13
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.

Sid
  • 560
  • 7
  • 17