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']);?>
burnedfacelessAsked:
Who is Participating?
 
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
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.

All Courses

From novice to tech pro — start learning today.