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

Learn how to contribute to open source projects in 7 easy steps (video)
Nov 27, 2022 programming

Learn how to contribute to open source projects in 7 easy steps (video)

we are going to talk about how to contribute to open source projects We will go through it together step by step ...

14 Min Read Read More
PHP Read and Write to Files
Mar 21, 2023 programming

PHP Read and Write to Files

Today we are going to learn about working with files in PHP.There are many functions that can help you read the contents of a file. we are going through the most common functions. ...

11 Min Read Read More
PHP array change key case
Feb 14, 2023 programming

PHP array change key case

Today we are going to talk about changing the case of the keys of an array in PHP. Sometimes you might change the case of the keys in your array. It's very easy to do that in PHP. ...

4 Min Read Read More
Top Skills for Developers: Non-Technical and Technical Essentials
Apr 05, 2023 programming

Top Skills for Developers: Non-Technical and Technical Essentials

Being a successful developer requires a combination of both technical and non-technical skills. ...

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