[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Question about include/include_once

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?
0
elepil
Asked:
elepil
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
Marco GasiFreelancerCommented:
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

script.php
<?php
include config.php;
include File1.php
...

Open in new window


Now suppose this is your config.php:

config.php
<?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.
0
 
SurranoCommented:
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:
http://www.php.net/manual/en/function.require.php
0
 
Ray PaseurCommented:
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:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.include-once.php
http://php.net/manual/en/function.require.php
http://php.net/manual/en/function.require-once.php

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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
elepilAuthor Commented:
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.
0
 
Ray PaseurCommented:
@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.
0
 
elepilAuthor Commented:
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.
0
 
SurranoCommented:
So you are lazy reading and I am lazy writing. Fair enough :)
0
 
elepilAuthor Commented:
Surrano,

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?
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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now