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


Question about include/include_once

Posted on 2014-01-20
Medium Priority
Last Modified: 2014-01-21
If a PHP page has two consecutive include statements like:

include File1.php
include File1.php

Will the code in File1.php get included twice? Because if it does, and File1.php contains functions, the result will be duplication of functions within the same PHP document.

But if it does not get included twice, wouldn't include_once become redundant?
Question by:elepil
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
  • 2
  • 2
  • +1
LVL 31

Accepted Solution

Marco Gasi earned 1600 total points
ID: 39796193
The code is included twice and if you call a function listed in File1.php you'll get a message lije this

Fatal error: Cannot redeclare sayhello() (previously declared in...

The same thing happens if File1.php is included in another file which is included in the current script. Let me explain. let's say you have a file called script.php

include config.php;
include File1.php

Open in new window

Now suppose this is your config.php:

include File1.php

Open in new window

Then when you'll call a function listed in File1.php you'll get the above message (if you have error_reporting correctly set)

So, if you're not sure a file is included one and only one time, use include_once instead.

Assisted Solution

Surrano earned 200 total points
ID: 39796194
This is exactly what include_once is designed for. Not only for function, but especially for class definitions.

Also study the difference between require(_once) and include(_once) here:
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39796379
Good news!  All of the PHP functions are documented, with examples and user-contributed notes, in the online man pages.  Whenever you are about to use a PHP function in your script, take a moment to read the man page and the user-contributed notes.  It will make a world of difference in the speed and accuracy of your work.  In the instant case, these are the pages you want to study:

Going forward if you want to learn PHP, this article will point you to some good learning resources and more importantly will help you avoid the many terrible PHP examples that litter the internet:
Technology Partners: 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!


Author Closing Comment

ID: 39797448
Just so you guys understand how I distribute points ...

When I ask a question here in Experts Exchange, it's because I want a QUICK and DIRECT answer. I give more points for such. Links referrals may or may not get points, depending on how helpful they are.

Thanks all for responding.
LVL 111

Expert Comment

by:Ray Paseur
ID: 39797496
@elepil: distribute points any way you want -- they're your virtual currency, after all, and I already have enough points to orbit Saturn.

Here is how I answer questions that scream out "I have never read the manuals!..."

I post links to the PHP man pages!  They are, after all, required reading for anyone who wants to write PHP scripts.  And there is a lot of good information in the PHP man pages.
explanations of the way the function works
its expected arguments
its possible return values
examples of usage
user-contributed notes
Taken together these things make PHP.net one of the most valuable resources a programmer can have.  So I try to encourage its use.  It's not that I'm lazy about answering questions, but some of the questions come up over and over again.  When I see that sort of thing in a larger context, I write an article about it.  But when the question is "how do I use a function" there is no better place to learn than from the authors of PHP.

Author Comment

ID: 39797623
Ray Paseur,

It's interesting that you quickly judged me as "lazy". Having posted many questions on this forum, I've encountered far too many responders who were "lazy" to give me a direct answer and just blindly copied and pasted text they found in Google. It really goes both ways.

But when all has been said and done, the key criterion is whether a question asked has been thoroughly answered or not. In this case, Marco Gasi was the only one who did just that, and in a very concise and direct fashion I might add. Responders should learn from him.

Your post provided a link to the PHP manual about "include". The page was lengthy and exhaustive in discussing "include", but nowhere does it say the error message that I would've gotten as Marco Gasi pointed out. So to follow your method, I would've spent an inordinate amount of time reading without really getting a direct answer to my question. Don't get me wrong, I'm not saying one should not read the manual, as everyone eventually should. But don't miss the forest for the trees --  that when one has a question, one seeks a direct and concise answer.

If you ask me for directions on a destination, do you want me to refer you to a map instead? Or would you appreciate it more if I gave you exact directions?

I still appreciate you responding, and I respect the method you are suggesting. But I hope you will be flexible enough to realize that there are more ways than one to arrive at a destination, and have the wisdom to respect how others approach their problems without being judgmental.

Expert Comment

ID: 39797828
So you are lazy reading and I am lazy writing. Fair enough :)

Author Comment

ID: 39798238

Actually I am not lazy reading in this case. I was just trying to find the shortest route from A to B.

As I mentioned to Ray Paseur, the link he provided about "include" was lengthy and did not directly address my problem. I would've spent so much time reading and end up empty-handed. If you examine Marco Gasi's response, you wouldn't find that in the link you or Ray Paseur provided.

I'm not saying I never consult the manual or do google searches, but in this case, I felt it easier to consult the more experienced on how "include" behaves. Nor am I saying I am never lazy, but in this case, I wasn't; I was merely trying to be more efficient in my pursuit of an answer.

I'm not sure why I'm even having this discussion on a board like this. This forum has a question and answer format. The more direct the answers, the more efficacious this forum would be. You and Ray Paseur did provide answers, but they were more of redirections than a direct answer to my problem. Now I don't know if you are like Ray who has "enough points to orbit Saturn", but if that were the case, what motivation would you have left to respond to other people? Is it not your desire to serve and help the community? And if so, wouldn't giving a very specific and direct answer serve your end better?

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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