[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 32
  • Last Modified:

Should I be using include instead of include_once?

<?php
include_once '../includes/functions.php';
$page = detect_page($_SERVER['SCRIPT_FILENAME']);
$mobile = is_mobile($_SERVER['SCRIPT_FILENAME']);?>
0
burnedfaceless
Asked:
burnedfaceless
  • 3
4 Solutions
 
Ray PaseurCommented:
Only if you want to include the script more than once.  If you just need it once, use include_once().
0
 
Ray PaseurCommented:
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
 
Olaf DoschkeSoftware DeveloperCommented:
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
 
Ray PaseurCommented:
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now