Link to home
Start Free TrialLog in
Avatar of rukiman
rukiman

asked on

include_once

I thought include_once was to only include your file once but execute the code more than once
so how come this doesn't run the code in a.php the second time.


include_once('a.php');
include_once('a.php');    <--- I expected the code to run again.

Anyway of doing this without having to refactor the code in a.php into a function that can be called twice?
SOLUTION
Avatar of HuyBD
HuyBD
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rukiman
rukiman

ASKER

So how come I had a line

echo '1'; in a.php

and this code:
include_once('a.php');
include_once('a.php');

only outputs 1 instead of 11?

That must mean the code did not get executed twice which is the result I want...
I am not sure what you are trying to do and why?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
note: probably what you are trying to do should be done using a class... just an idea.
Avatar of rukiman

ASKER

problem with

include('a.php');
include('a.php');

is that there are warnings of functions redeclarations... etc..

So your saying that include_once like in the case below will only execute the code in a.php once and thats it? If you read the PHP manual it says this is good for include a file once but executing the code more than once...what did they mean by this?

include_once('a.php');
include_once('a.php');


The reason I want to do this is that a.php works out some outputs from some inputs, but at the same time works out some required inputs that are only known because it depends on the output and hence the reason to execute the script another time so get the net result output. Hope that makes sense....I could do this by making the code in a.php a function but that would be a bit messier....

>If you read the PHP manual it says this is good for include a file once but executing the code more than once.
I cannot find that in the php manual...

you should put the include_once('a.php') ONCE in your code,
and any code that you want to be executed twice should be in a function, and simply call that function twice.
Avatar of rukiman

ASKER

I know that would be the ideal way, but didn't want to go down that path because otherwise the function in a.php would be huge.

I fixed my problem by doing this:

a.php  ---- code i want executed only i.e echo('d'); and also include_once('a_func.php');
a_func.php   --- all the functions that used to belong in a.php

then from the external script
include('a.php');
include('a.php');

does what I want now, it prints dd. And since all the functions are only included once I do not have redeclaraction issues.

Thanks all for your input. I will award the answers that helped work this out.