PHP

How to use require – include require_once and include_once in PHP

April 23, 2022

How to use require – include require_once and include_once in PHP

In this article we will learn how to use require – include require_once and include_once, PHP command to insert files

require – include require_once and include_once in PHP thumb

Hey you programmer, how are you? Let’s learn how to use requires and includes in PHP!

First, it is good to understand that these instructions are exclusively for adding files to another file in PHP

So here we go, first the difference between include and require: they do exactly the same function, but the type of error ends up being different when the instruction is not executed successfully

The require will terminate the execution of the script that is running, the include will only generate a warning and continue its execution

See the type of error generated by the instructions:

PHP Warning: include(): Failed opening 'file.php' for inclusion

PHP Fatal error: require(): Failed opening required 'file.php'

Note that the warning will not stop your software, the Fatal Error will

The difference between include_once and require_once

Once again, the commands represent the same function, they will include the files determined by the parameter, so both have the same function

But the added functionality of the two once statements is that they ensure that a file is only included once in your software.

For example a page footer template, using once you ensure that only one footer is included

Also in these commands there is a confusion between single and double quotes, we recommend reading this article to understand them better 🙂

Conclusion

In this article we learned how to use require – include require_once and include_once in PHP

All instructions have the same objective: include a file to this PHP file that we are using the inclusion rule

The difference from the once statements is that they guarantee the inclusion of a given file at once

Do you want to learn more about PHP and web development? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x