Solved

Should I be using include instead of include_once?

Posted on 2016-09-11
4
25 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 109

Accepted Solution

by:
Ray Paseur earned 250 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 250 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 109

Assisted Solution

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

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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