What works for me is using the “_once” versions of include and require. TBH, I can understand the difference between include (don’t crash if it fails for some reason) and require (if it fails, STOP the code). But I can’t comprehend why there would need to be once and not once versions. The only guess I have is that the once is more load because it checks to see if it already exists which might be unnecessary, but I don’t imagine it would be a significant load.
The problem here is that regardless if you use _once or the regular include/ require, it will always be called. I ran into this issue when I first started PHP. but what solved my problem was that I had to wrap the function within an if statement. Here’s a snippet example
if(!function_exists('myFunctionName')) {
function myFunctionName() {
// Declare the function stuff here
}
}
If I had to guess you probably are trying to require the file from within your function.
In any event, sometimes it helps to create a simple test case:
But do yourself a favor and don’t do this. You are already on shaky grounds just using require statements. Globals tend to be very evil. Just pass your arguments as function parameters. If you have a bunch then pass them as an array.
This might be a bit overkill for now but might help in organizing your app:
When a PHP file is included, the code within is run immediately “inline” where it is included.
The main reason you don’t always use “once” is because you might be including a partial. I had a site once where they had a PHP file that controlled an SVG master symbol library. So they would include() the PHP file and it would run logic to determine which SVG symbol is output. So this PHP file could be included multiple times in a page.
PHP can be included for any kind of output needed, not just function/class definition files.
Only if your PHP file contains ONLY functions and classes and such, should it be using “once”. And it should be included at the global scope, not from within a function or class.
In other words, this person using procedural style programming. Stuff should be included as needed within flow of how the code is processed. But if their includes are just function definitions, they should be included early in the app session. There should be no reason to need “once” if it’s programmed in the correct procedural way.
In any event, so you have page1.php, page2.php and page3.php. Are these loaded inline in the code or are they each independent endpoints? Like example.com/page1.php and example.com/page2.php?
If they are unique endpoints, then just put all your functions in some include file like helper-functions.php and include it from each file.
If they are not independent, a user only goes to example.com/page1.php and then this loads the other pages as needed, this is where you may get in trouble. But it can still be fixed by simply putting all your functions in a functions file and load it early so that the functions are all available to whatever file needs them.