From the php manual
Include_once may help avoid problems such as function redefinitions, variable value reassignments, etc.
Ok, so include_once solves issues with function redefinitions, variable value reassignments, etc. but why are they an issue in the first place ?
I’m trying to understand what kind of risks are involved in redefining functions or reassigning variable values except for a decline in performance due to additional input/output and processing ?
Is it because php parser gets confused which version of function to load/use or is the original version of the function lost once redefined? What else and what about variable reassignments?
I do understand where to use include vs include_once.
Imagine the following include file,
hello.php:Now imagine the following file,
index.php:Your code would now have a fatal error, since the function has been defined twice. Using
include_oncewould avert this, since it would only includehello.phponce. Also, to do withvariable value reassignment,$a(should the code compile) would be reset to 0.From the comments, please consider this a side answer – If you’re looking for something where resetting a set of variables many times was required, I’d look to use a class for this with a method like
Reset, you can even make it static if you didn’t want to have to instantiate it, like so:Usage like: