Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Difference Between Include, Require, Once in PHP

INTRODUCTION
SAME BUT DIFFERENT

Welcome to a tutorial on the difference between Include, require, include_once, and require_once in PHP. If you have just started learning PHP, then you would have probably heard of using include and require to “load” an external script. While both of them may seemingly do the same thing, there is actually a slight difference between them.

Also, there is the include_once and require_once. Just what is going on here? Why are there so many different versions? What are the differences, and when should we use them? Read on to find out!

I have included a zip file with all the example source Code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Section A
The Basics

Section B
Extra Examples

Extra
Useful Bits

Extra
Source Code Download

Closing
What’s Next?

SECTION A
THE BASICS

Let us first start with the basics, and take a closer look at each one of the functions – At what each of them does and their differences.

DIFFERENCES IN A NUTSHELL

  • require Where a file is required by your script, will throw a fatal error if the specified file is not found (or is not readable). Stops any further processing on a missing file.
  • include Where an optional file is needed by your script. Will throw a warning if the specified file is not found (or is not readable). Will continue processing on a missing file.
  • require_once Same as require, but will checks if the specified file has already been loaded before.
  • include_once Same as include, but will checks if the specified file has already been loaded before.

INCLUDE EXAMPLE

As you code ninjas already know, we use include to load another file into our script. So let us begin the example with a simple dummy class file.

1-class.php

Then, we use include to load it in our main script.

2-include.php
speak();
?>

Yep, include is that simple, but take extra note on how it continues to run even if the specified file is not found.

REQUIRE EXAMPLE

require is the strict sibling of include, and it will halt all further processing on a missing file.

2-include.php
speak();
?>

INCLUDE_ONCE AND REQUIRE_ONCE

4-include-require-once.php
speak();
?>

WHEN TO USE WHAT?

Now that you know the differences between all of these siblings, when should we use each one? Well, it all depends on the situation.

  • Use require if the external file is critical to further processing. E.g. Loading a library file.
  • Use include if the external file is optional for further processing. E.g. Loading a log file or some additional statistic.
  • require_once and include_once if the file may have been loaded somewhere else before.
  • There has been some past debate that require_once and include_once don’t perform very well. That they are slow, has to check if the file is already loaded – But the modern versions of PHP proved otherwise. While they do not perform as well, but they are still very decent. 
  • I will personally not recommend using require_once and include_once though… It is kind of a bad lazy way to prevent good programming practices and structuring.

SECTION B
EXTRA EXAMPLES

Before you dismiss require and include as “simple”, there are a couple more things to look out for, and we will run through them in this section.

CAN LOAD ANY FILE TYPE

Notice that I use “include a file”, and not “include a script” above? Yep, we can pretty much load any kind of files with require and include – 

5a-any-file.php


  
    
      Include require demo
    
5b-css.css
html, body {
  font-family: arial;
  font-size: 2em;
}
5c-html.html

This is formatted in HTML.

5d-text.txt
This is just plain text.

SEQUENCE DOES MATTER

Common newbie mistake – You have to remember that PHP is read sequentially, from top to bottom, left to right. So please don’t use a class before you load it.

6a-bad.php
speak();
require "1-class.php";
?>
6b-good.php
speak();
?>

INCLUDE NOT WORKING – CASE SENSITIVE

So, you have properly included the file in a Windows setup and it works perfectly. But once on a Linux machine, it fails miserably. Just why is that so? Two words – case sensitive.

7a-case-sensitive.php
7b-FooBar.php

EXTRA
USEFUL BITS

That’s all for this project, and here is a small section on some extras that may be useful to you.

THE QUICK INFOGRAPHIC

Include vs Require in PHP (click to enlarge)

REFERENCES

  • Official PHP manual – require
  • Official PHP manual – include
  • require_once
  • include_once

ABSOLUTE VS RELATIVE PATH

Still having trouble with “missing files”? That may be a file path issude, and check out why in my other guide:

Absolute and Relative Paths in PHP – A Comprehensive Guide

EXTRA
DOWNLOAD

Finally, here is the download link to the example source code as promised.

QUICK START

Skipped the entire tutorial? There is no database setup nor anything “special”, so just download and unzip into a temporary folder – Then run through each of the example files.

SOURCE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
 

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

The post Difference Between Include, Require, Once in PHP appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

Difference Between Include, Require, Once in PHP

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×