4

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.

1
  • Sure you can. Why isn't it working? Commented Jan 15, 2017 at 17:25

2 Answers 2

3

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');
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.