Home » Difference Of require, include, require_once And include_once

Difference Of require, include, require_once And include_once

Last updated on May 25, 2021 by

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:

  1. How to Select Data Between Two Dates in MySQL
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How to Convert Base64 to Image in PHP
  12. Check If A String Contains A Specific Word In PHP
  13. Dynamically Populate A Select Field’s Choices In ACF
  14. How To Find Duplicate Records in Database
  15. 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!

 
 

Leave a Comment