Solved

Question about include/include_once

Posted on 2014-01-20
8
259 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 30

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 108

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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

18 Experts available now in Live!

Get 1:1 Help Now