Solved

Should I be using include instead of include_once?

Posted on 2016-09-11
4
23 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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
Only if you want to include the script more than once.  If you just need it once, use include_once().
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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…
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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now