Solved

Question about include/include_once

Posted on 2014-01-20
8
267 Views
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?
0
Comment
Question by:elepil
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 400 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

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
 
LVL 8

Assisted Solution

by:Surrano
Surrano earned 50 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:
http://www.php.net/manual/en/function.require.php
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 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:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:elepil
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.
0
 
LVL 109

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

Author Comment

by:elepil
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.
0
 
LVL 8

Expert Comment

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

Author Comment

by:elepil
ID: 39798238
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 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