Johnenzone
Johnenzone

Reputation: 59

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.

Upvotes: 4

Views: 5672

Answers (2)

a55
a55

Reputation: 556

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');

Upvotes: 3

Sid
Sid

Reputation: 560

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.

Upvotes: 1

Related Questions