file a.html
<div id=1>
<?php Include "a.php?parameter=1" ?>
</div>
<div id=2>
<?php Include "a.php?parameter=2" ?>
</div>
file a.php:
<? Php
b;
Function b {}
?>
calling a.html produces the error: “cannot redeclare b()” in div 2. The reason is easily understood – but what is the most efficient workaround?
Including another PHP file doesn’t fire off a new HTTP request. You cannot pass parameters to the included file like that. The text inside the quotes must resolve to a file on disk, including the
?parameter=1part.If you want to pass parameters to the included file, keep in mind that the current scope is available to the included code. You can assign values to global/local variables and then use them within the included file.
Update
You cannot declare the same function more than once, even when using
include. Instead, include the file that declares the function at the top of your page once. Userequire_onceto ensure it happens only once and breaks if it cannot find the file. Then, anywhere you need the effects of the function, call it in your page’s script without doing another include.This creates the desired output.