?
Solved

include_once

Posted on 2006-11-12
9
Medium Priority
?
207 Views
Last Modified: 2013-11-18
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?
0
Comment
Question by:rukiman
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 17

Assisted Solution

by:HuyBD
HuyBD earned 123 total points
ID: 17928024
the include_once is issue to prevent constant or function redefinitions, so can use include to call twice
0
 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 123 total points
ID: 17928029
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
 

Author Comment

by:rukiman
ID: 17928046
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 17928137
I am not sure what you are trying to do and why?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 129 total points
ID: 17928163
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17928164
note: probably what you are trying to do should be done using a class... just an idea.
0
 

Author Comment

by:rukiman
ID: 17928180
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17928190
>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
 

Author Comment

by:rukiman
ID: 17934691
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question