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 Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP break and continue
Mar 21, 2023 programming

PHP break and continue

Today we are going to learn about break and continue in PHP. These two words are going to help you a lot when dealing working with loops. you can avoid a lot of extra iterations and save time. ...

5 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 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 ...