Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array_walk require_once

Tags:

arrays

php

I was just wondering if anyone knew why I can't use require_once as a callback for array_walk. I can just include it in an anonymous function and run that, but it gives an invalid callback error for the usage:

$includes = array(
    'file1.php',
    'file2.php',
    'file3.php'
);
array_walk($includes, 'require_once');
like image 400
Peter Avatar asked Dec 20 '22 19:12

Peter


2 Answers

You are going to waste more time finding out what's wrong. Just use:

$includes = [
    'file1.php',
    'file2.php',
    'file3.php'
];
foreach($includes as $include) {
    require_once($include);
}
like image 39
ziGi Avatar answered Dec 24 '22 01:12

ziGi


require_once is not a PHP function, but a control structure.

like image 93
Martin Avatar answered Dec 24 '22 00:12

Martin