require multiple files in php

post image

In this tutorial, I'll show you how you can require multiple files in PHP

to start, you'll need to create a PHP file, for this tutorial I'll call it tutorial.php


Step 1: create a variable

First, you'll need to create a variable:

$variable = glob(./files/*.php);

The glob() function is a built-in function in PHP and can look into directories for files.

in this case, it will look in the directory where tutorial.php is stored, And then into the ./files directory.

on a Linux server that is usually is /var/www/html/tutorial.php.

meaning it will look inside /var/www/html/files,

and assign all the files from the files directory to an array.


Step 2: create a Foreach loop

Second, you'll need to create a foreach loop:

foreach ($variable AS $files) {

    require "$files";


A foreach loop will loop through an array and assign 1 by 1 each piece of data

to, in this case, a variable called $files


Step 3: DONE!

your code should now look like this:


$variable = glob(./files/*.php);

foreach ($variable AS $files) {

    require "$files";


If the PHP code is displayed in your web browser try turning on or installing the program PHP on your server,

or try to rename tutorial.html to tutorial.php

or try adding the PHP tag (<?php) to the top of your code