Home > Article > Backend Development > How to Include All PHP Files from a Directory?
Including All PHP Files from a Directory
Including multiple PHP files manually can be tedious, especially when working with numerous sub-classes. Is there a way to include an entire directory of PHP scripts, similar to using include('classes/*')?
Solution:
PHP provides the glob() function to retrieve a list of files matching a specific pattern. Using glob() and a foreach loop, you can include all PHP files from a directory as follows:
<code class="php"><?php foreach (glob("classes/*.php") as $filename) { include $filename; } ?></code>
Explanation:
The glob("classes/*.php") statement returns an array of all PHP files located in the classes directory. The foreach loop then iterates through each filename, including each file using the include statement. This technique allows you to include multiple files without the need for manual inclusion.
The above is the detailed content of How to Include All PHP Files from a Directory?. For more information, please follow other related articles on the PHP Chinese website!