Free cookie consent management tool by TermsFeed Generator PHP Include and Require | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Include and Require

PHP Include and Require

Last Updated on Mar 21, 2023

What do they do?

include/require will take all the code/content from an specified file and put it in your current file.

For example let’s say I have a file like this

<?php
echo "Hello Pratham!";

And My current file is like this:

<?php
echo "Hello World!";

If I include/require the first file in my current file it will echo both of the statements.

The order of them depends on where I include the other file

<?php
include "./name.php";
echo "<br>";
echo "Hello World!";
// output is:
// Hello Pratham!
// Hello World!

and

<?php
echo "Hello World!";
echo "<br>";
include "./name.php";
// output is:
// Hello World!
// Hello Pratham!

In both cases we could use require as well.

Include vs. Require

So what’s the difference between include and require?

If ther's an error, include shows a warning and then the rest of your script keeps running.

But require will throw an error and stops the execution of the code

Use case

"so what's the use case?" you might ask.

well one of the most common use cases is that you can easily organize your files and avoid repeating yourself.

for example you can have footer and header and sidebar in a seperate file and include them in your files. that way you don't have to repeat yourself and you can update your footer or header in one place and your code will be updated everywhere

<?php
require './header.php';
require './sidebar.php';
// main content goes here
require './footer.php';

Include once

when you use include_once or require_once, PHP will keep track of the files, and if you have already included/required a file, it won't include/require it again. this way PHP helps you avoid issues like function redefinition, etc. other than that their behaviour is identical.

https://youtu.be/yy8QogjpuiI

Conclusion

Now you know about working with include and require in PHP.

I recommend you to open multiple PHP files and try to include/require their content inside another file. include/require them multiple times to see if you get any errors.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • working with include and require
  • include vs require
  • include vs include once
  • require vs require once

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP Array Combine
Feb 14, 2023 programming

PHP Array Combine

Today we are going to talk about array combine in PHP. Array combine is another useful array function that creates an array by using one array for keys and another for its values. ...

3 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 Min Read Read More
How to Combine Excel Files Using Python
May 08, 2024 programming

How to Combine Excel Files Using Python

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with Python can be easily done. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...