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 |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |