Home  >  Article  >  Backend Development  >  How to Include All PHP Files from a Directory?

How to Include All PHP Files from a Directory?

Barbara Streisand
Barbara StreisandOriginal
2024-11-03 10:45:03706browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn