Include Functions in PHP

Include Functions in PHP

Including files in PHP is a handy feature when you want to include the same content or design in multiple pages such as header, footer, sidebar, etc.  In PHP, four include functions available, which we will learn and see the difference between them.

1.include() function: In the include function, write your file path where your file exists. In the include() function, if we write the wrong path sometimes due to typing mistake, then it will throw an error on the particular area where you are trying to include the file rest of code will be working fine.

Example: include(‘includes/header.php’);

This example includes folder name, and header.php is the file name.

2.require() function: In require function, simply write your file path where your file exists. In require() method, if we write the wrong path due to typing mistake, then it will throw an error, and it halts the execution of complete code means your file will not work.

Example: require(‘includes/header.php’);

3.include_once() function: include_once() is improved version of include() function. It is latest include function, sometimes we include file multiple time by mistake then it will help. It will not repeat our design means it does not accept the same file multiple times.

Example: include_once(‘includes/header.php’);

4.require_once() function: require_once() is an improved version of require function work same require, but there is a slight improvement; it does not accept the same file multiple times.

Example: require_once(‘includes/header.php’);