[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

include_once

Posted on 2006-11-12
9
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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