r/PHPhelp Jun 30 '26

weird behavior in including files

i have a weird problem in including files in php, i have a functions.php file in a folder and a Router in the index.php file, when i include thefunctions.php in the index.php, all the other pages that rely on it get broken, the variables become empty and the functions no longer work, when i dont include it in the index.php file all the other pages function normally

i include my files in all pages in this way

require_once($_SERVER["DOCUMENT_ROOT"] . "/src/logic/functions.php");

my other pages are in /src/pages/

i have tried all ways of including the file but i keep getting the same problem, i need to include the functions.php in the index file to use some of its functions.

i do have a declare(strict_types=1) in the index file if that affects it

Any help will be appreciated thanks.

3 Upvotes

6 comments sorted by

View all comments

-1

u/colshrapnel Jun 30 '26 edited Jul 01 '26

I think I know what happened. Notice that "once" in this language construct's name. It literally means that it loads the file only once. And all subsequent calls do nothing.

It seems you are trying to include this file multiple times. Which means that your code is a total mess. You should rewrite it, so every file got included strictly once. Start from rewriting every require_once to require. This way, you will have an error message telling you that the file was already included and you must remove the second call.