Often, developers get confused between require, include, require_once and include_once because they look the same but they are a bit different. Let’s see how they are different.
Difference Between require, include, require_once And include_once
01 include() In PHP
include() statement is used to include a .php file into another PHP file. For example, if you have a main.php
file and you want to include header.php
and footer.php
then you can include using include()
statement.
include() will only produce a warning (E_WARNING) and the script will be continue.
main.php
<?php
include('header.php');
// Other HTML,CSS, JS and PHP Code
include('footer.php');
02 include_once() In PHP
The include_once() statement is identical to include()
but PHP will check if the file has already been included then that file will not include again.
include_once() will only produce a warning (E_WARNING) and the script will continue.
main.php
<?php
include_once('header.php'); // Added by you
include_once('header.php'); // Mistakely added
// Other HTML,CSS, JS and PHP Code
include('footer.php');
Sometimes, it is possible that you have thousands of lines of code in your file and you mistakenly included the same file many times then include_once() include the file only a single time.
03 require() In PHP
The require() statement is also identical to include()
that means it is also used to include files into other PHP files.
Difference is require() will only produce a fatal E_COMPILE_ERROR and it will halt the script.
main.php
<?php
include('header.php');
// Other HTML,CSS, JS and PHP Code
include('footer.php');
04 require_once() In PHP
The require_once statement is also identical to require
except PHP will check if the file has already been included then that file will not include again.
require_once() will only produce a fatal E_COMPILE_ERROR and it will halt the script.
main.php
<?php
require_once('header.php'); // Added by you
require_once('header.php'); // Mistakely added // PHP will ignore the file
// Other HTML,CSS, JS and PHP Code
require_once('footer.php');
Additionally, read our guide:
- How to Select Data Between Two Dates in MySQL
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- Dynamically Populate A Select Field’s Choices In ACF
- How To Find Duplicate Records in Database
- Difference Between composer.json And package.json
That’s it from our end. We hope this article helped you to understand the difference between require, include, require_once and include_once.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!