PHP, is require_once the only thing we need, the rest are redundant?

I am writing my first PHP application, and I noticed that I've been doing pretty well with just require_once. It ensures the code I'm trying to include is included, and it never duplicates.

But being new to PHP, am I wrong to think this? Can anyone give me a situation where require, include or include_once would be better?

To responders, please don't give me a link to the online PHP manual, I've seen it. My purpose for making this post is just to get a feel if other PHP programmers feel the same way, and if not, I'm hoping to get their thoughts.

Thanks.
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
'require_once' is good when you are working on just part of a PHP application and there is a chance that another file will try to include that same file.  Same for 'include_once'.  None of the files and pages I work on have more than one level of 'include' so any of the variations work fine for me.
0
elepilAuthor Commented:
Dave, thanks for responding.

What I wanted to hear from other experienced PHP developers though was whether they feel the same way -- that require_once is all we need, the rest are redundant.

Would you agree?
0
Dave BaldwinFixer of ProblemsCommented:
No, I'm sure the authors thought there was a reason for them. I never have a reason to think about it.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Chris StanyonWebDevCommented:
I'll often use 'include' for presentational stuff, like templating, but use 'require' if it's for functional stuff, like additional functions, classes etc. As you've probably already read - the difference is simply in the error handling (notice vs fatal)

Often there is an absolute need to include the same file multiple times (again, templating for example), so you obviously wouldn't use the *_once version there. Other times, your application may break if you include a file more than once.

The simple answer to your question is, No - the rest aren't redundant.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
When I'm templating a web fragment and could use include() repeatedly to bring in the "partial" elements, I find it is faster to use a strategy like HEREDOC.  Your script does not need to go back to the server file system for more copies of mutable document fragments; these can be defined in PHP variables, and a single include() can do the job.  Note that template fragments you include via HEREDOC need to be in-line for the proper variable substitutions to occur.

As you get more advanced with PHP and consider the possibility of building and deploying applications, you may want to learn about the 21st century approach to this problem.  It helps you move in the direction of OOP design principles.

The Standard PHP Library has a lot of good things to offer, but it seems many PHP programmers never get beyond procedural programming, so they never get the advantages of object-oriented design in PHP.  Why use autoloaders instead of include() variants?  Among other things, they respect namespaces.  You don't need that power if you're just putting together a simple web page, but you do need it if you're planning to publish a package on GitHub, or use the packages published on GitHub.
0
elepilAuthor Commented:
Chris Stanyon,

I didn't think of templating, you would be right that you'd need to include two identical things at the same time. Thanks for your concise and well-stated opinion and example.

Ray Paseur/Dave Baldwin,

Thank you both for your opinions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.