Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Should I be using include instead of include_once?

Posted on 2016-09-11
4
Medium Priority
?
30 Views
Last Modified: 2016-09-11
<?php
include_once '../includes/functions.php';
$page = detect_page($_SERVER['SCRIPT_FILENAME']);
$mobile = is_mobile($_SERVER['SCRIPT_FILENAME']);?>
0
Comment
Question by:burnedfaceless
  • 3
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 41793411
Only if you want to include the script more than once.  If you just need it once, use include_once().
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 41793412
You might also think about require() which is basically the same as include() but will cause a run-time error if the script cannot be loaded.
0
 
LVL 30

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 1000 total points
ID: 41793414
The strictest include method is require_once. The ...once variants don't allow to include twice, which can be overseen in badly written lengthy scripts and lead to redefinition of variables which should not occur. The require variants make it a requirement and let the whole script fail, if the required file is not found.

In cases you want to include an external files multiple times, eg repeat a snippet of HTML, include is the right choice. Since this functions.php most probably (you don't tell) is about function definitions you use in following code, you a) require them and b) don't need the definitions multiple times, so for this require_once seems the best fit.

Just look into php.net in this case, it's all explained there.

Bye, Olaf.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 41793436
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

772 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