Can we include a file two times in any PHP page?

Yes, you can use include (or require) for the same file many times. PHP will add its code every time.

But there is a problem if that file declares a function or a class. The second include tries to create the same function again, and PHP stops with a fatal error like “Cannot redeclare function”.

<?php
include "helpers.php";
include "helpers.php";        // error if helpers.php has functions

include_once "helpers.php";   // safe: adds the file only one time
?>

Use include_once or require_once for files with functions, classes or settings. They check if the file was already added. See PHP File Inclusion.


Learn more about the similar topics:
Tutorials
PHP Advanced
PHP File Upload
PHP File Handling
PHP File Inclusion
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is include() function in PHP?
Can we include a file two times in any PHP page?
Which is the PHP function to delete a file from server?
What is fopen function PHP?
What is the difference between unlink() and unset() in PHP?
How to check if a file exists in PHP?
How to get the size of a file in PHP?