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?
rukimanAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
if you do this:
include('a.php');
include('a.php');

you will get 11 instead of 1.

if you read carefully what has been written above, you would have understood that include_once will include the file only ONCE. if you try to do it a second time, it will not do it. full stop.
0
 
HuyBDConnect With a Mentor Commented:
the include_once is issue to prevent constant or function redefinitions, so can use include to call twice
0
 
Aamir SaeedConnect With a Mentor Commented:
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

include_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.


Example 16-13. include_once() is case insensitive on Windows

<?php
   include_once("a.php"); // this will include a.php
   include_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)
?>

This behaviour changed in PHP 5 - the path is normalized first so that C:\PROGRA~1\A.php is realized the same as C:\Program Files\a.php and the file is included just once.

One of the problems caused by using multiple libraries within your code is the danger of calling include() twice on the same file. This can occur in larger projects when different library files call include() on a common file. Including the same file twice will often result in the repeated declaration of functions and classes, thereby causing the PHP engine great unhappiness.

The situation is saved by the include_once() statement. include_once() requires the path to an include file and will behave the same way as include() the first time it is called. If include_once() is called again for the same file during script execution, however, the file will not be included again.

This makes include_once() an excellent tool for the creation of reusable code libraries.

- BOL -
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
rukimanAuthor Commented:
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...
0
 
Aamir SaeedCommented:
I am not sure what you are trying to do and why?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
note: probably what you are trying to do should be done using a class... just an idea.
0
 
rukimanAuthor Commented:
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....

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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.
0
 
rukimanAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.