Solved

include_once

Posted on 2006-11-12
9
195 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 41 total points
Comment Utility
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 41 total points
Comment Utility
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
Comment Utility
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
 
LVL 14

Expert Comment

by:Aamir Saeed
Comment Utility
I am not sure what you are trying to do and why?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 43 total points
Comment Utility
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 142

Expert Comment

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

Author Comment

by:rukiman
Comment Utility
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 142

Expert Comment

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress type image upload 10 25
html input clean up 3 28
while loop in html mail format 5 32
Filter wordpress query 3 5
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now