define a directory for including a file












1















I'm currently working on my first independent project, I'm looking to keeping my code to the best standard possible, I've been looking in the PHP manual but I seem to be missing how to cleanly define a directory to include, for example my templates folder is 'templates/default' and I'd like to define that once.



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


if anyone could guide me in the right direction that would be much appreciated, thanks










share|improve this question




















  • 1





    require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

    – Nunchy
    Nov 15 '18 at 18:02


















1















I'm currently working on my first independent project, I'm looking to keeping my code to the best standard possible, I've been looking in the PHP manual but I seem to be missing how to cleanly define a directory to include, for example my templates folder is 'templates/default' and I'd like to define that once.



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


if anyone could guide me in the right direction that would be much appreciated, thanks










share|improve this question




















  • 1





    require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

    – Nunchy
    Nov 15 '18 at 18:02
















1












1








1


1






I'm currently working on my first independent project, I'm looking to keeping my code to the best standard possible, I've been looking in the PHP manual but I seem to be missing how to cleanly define a directory to include, for example my templates folder is 'templates/default' and I'd like to define that once.



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


if anyone could guide me in the right direction that would be much appreciated, thanks










share|improve this question
















I'm currently working on my first independent project, I'm looking to keeping my code to the best standard possible, I've been looking in the PHP manual but I seem to be missing how to cleanly define a directory to include, for example my templates folder is 'templates/default' and I'd like to define that once.



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


if anyone could guide me in the right direction that would be much appreciated, thanks







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:58







coding_with_tea

















asked Nov 15 '18 at 17:53









coding_with_teacoding_with_tea

83




83








  • 1





    require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

    – Nunchy
    Nov 15 '18 at 18:02
















  • 1





    require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

    – Nunchy
    Nov 15 '18 at 18:02










1




1





require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

– Nunchy
Nov 15 '18 at 18:02







require_once(__DIR__ . "/" . TEMPDIR . "/header.php");

– Nunchy
Nov 15 '18 at 18:02














2 Answers
2






active

oldest

votes


















2














Welcome to Stack.



I guess that you want to define a constant TEMPDIR and then use it in the require_once().



To define a variable please use define() (please see manual https://secure.php.net/manual/en/language.constants.php):



<?php define('TEMPDIR', 'templates/default');


To use constant in the require_once() you have to do a string concatenation (please see manual http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):



<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');


You should also consider using DIRECTORY_SEPARATOR constant to make sure that you use separators which will work on any OS (please see manual http://php.net/manual/en/dir.constants.php):



<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');


Cheers!






share|improve this answer


























  • Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

    – coding_with_tea
    Nov 15 '18 at 18:14



















0














The require line is treating TEMPDIR as part of a string literal:



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


Try:



 TEMPDIR = templates/default
require_once (__DIR__ . '/' . TEMPDIR . '/header.php');





share|improve this answer
























  • thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

    – coding_with_tea
    Nov 15 '18 at 18:08











  • Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

    – Nunchy
    Nov 15 '18 at 18:09











  • it has the contents of a html header document inside

    – coding_with_tea
    Nov 15 '18 at 18:10











  • I see, but how are those contents being output? Can you post the file?

    – Nunchy
    Nov 15 '18 at 18:11






  • 1





    it's been solved by Damian Dziaduch above, thanks.

    – coding_with_tea
    Nov 15 '18 at 18:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325300%2fdefine-a-directory-for-including-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Welcome to Stack.



I guess that you want to define a constant TEMPDIR and then use it in the require_once().



To define a variable please use define() (please see manual https://secure.php.net/manual/en/language.constants.php):



<?php define('TEMPDIR', 'templates/default');


To use constant in the require_once() you have to do a string concatenation (please see manual http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):



<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');


You should also consider using DIRECTORY_SEPARATOR constant to make sure that you use separators which will work on any OS (please see manual http://php.net/manual/en/dir.constants.php):



<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');


Cheers!






share|improve this answer


























  • Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

    – coding_with_tea
    Nov 15 '18 at 18:14
















2














Welcome to Stack.



I guess that you want to define a constant TEMPDIR and then use it in the require_once().



To define a variable please use define() (please see manual https://secure.php.net/manual/en/language.constants.php):



<?php define('TEMPDIR', 'templates/default');


To use constant in the require_once() you have to do a string concatenation (please see manual http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):



<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');


You should also consider using DIRECTORY_SEPARATOR constant to make sure that you use separators which will work on any OS (please see manual http://php.net/manual/en/dir.constants.php):



<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');


Cheers!






share|improve this answer


























  • Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

    – coding_with_tea
    Nov 15 '18 at 18:14














2












2








2







Welcome to Stack.



I guess that you want to define a constant TEMPDIR and then use it in the require_once().



To define a variable please use define() (please see manual https://secure.php.net/manual/en/language.constants.php):



<?php define('TEMPDIR', 'templates/default');


To use constant in the require_once() you have to do a string concatenation (please see manual http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):



<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');


You should also consider using DIRECTORY_SEPARATOR constant to make sure that you use separators which will work on any OS (please see manual http://php.net/manual/en/dir.constants.php):



<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');


Cheers!






share|improve this answer















Welcome to Stack.



I guess that you want to define a constant TEMPDIR and then use it in the require_once().



To define a variable please use define() (please see manual https://secure.php.net/manual/en/language.constants.php):



<?php define('TEMPDIR', 'templates/default');


To use constant in the require_once() you have to do a string concatenation (please see manual http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):



<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');


You should also consider using DIRECTORY_SEPARATOR constant to make sure that you use separators which will work on any OS (please see manual http://php.net/manual/en/dir.constants.php):



<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');


Cheers!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 3:41

























answered Nov 15 '18 at 18:10









Damian DziaduchDamian Dziaduch

784611




784611













  • Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

    – coding_with_tea
    Nov 15 '18 at 18:14



















  • Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

    – coding_with_tea
    Nov 15 '18 at 18:14

















Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

– coding_with_tea
Nov 15 '18 at 18:14





Fantastic, that's just what I was looking for, thank you very much, I really appreciate your help.

– coding_with_tea
Nov 15 '18 at 18:14













0














The require line is treating TEMPDIR as part of a string literal:



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


Try:



 TEMPDIR = templates/default
require_once (__DIR__ . '/' . TEMPDIR . '/header.php');





share|improve this answer
























  • thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

    – coding_with_tea
    Nov 15 '18 at 18:08











  • Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

    – Nunchy
    Nov 15 '18 at 18:09











  • it has the contents of a html header document inside

    – coding_with_tea
    Nov 15 '18 at 18:10











  • I see, but how are those contents being output? Can you post the file?

    – Nunchy
    Nov 15 '18 at 18:11






  • 1





    it's been solved by Damian Dziaduch above, thanks.

    – coding_with_tea
    Nov 15 '18 at 18:14
















0














The require line is treating TEMPDIR as part of a string literal:



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


Try:



 TEMPDIR = templates/default
require_once (__DIR__ . '/' . TEMPDIR . '/header.php');





share|improve this answer
























  • thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

    – coding_with_tea
    Nov 15 '18 at 18:08











  • Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

    – Nunchy
    Nov 15 '18 at 18:09











  • it has the contents of a html header document inside

    – coding_with_tea
    Nov 15 '18 at 18:10











  • I see, but how are those contents being output? Can you post the file?

    – Nunchy
    Nov 15 '18 at 18:11






  • 1





    it's been solved by Damian Dziaduch above, thanks.

    – coding_with_tea
    Nov 15 '18 at 18:14














0












0








0







The require line is treating TEMPDIR as part of a string literal:



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


Try:



 TEMPDIR = templates/default
require_once (__DIR__ . '/' . TEMPDIR . '/header.php');





share|improve this answer













The require line is treating TEMPDIR as part of a string literal:



TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');


Try:



 TEMPDIR = templates/default
require_once (__DIR__ . '/' . TEMPDIR . '/header.php');






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 18:05









NunchyNunchy

825411




825411













  • thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

    – coding_with_tea
    Nov 15 '18 at 18:08











  • Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

    – Nunchy
    Nov 15 '18 at 18:09











  • it has the contents of a html header document inside

    – coding_with_tea
    Nov 15 '18 at 18:10











  • I see, but how are those contents being output? Can you post the file?

    – Nunchy
    Nov 15 '18 at 18:11






  • 1





    it's been solved by Damian Dziaduch above, thanks.

    – coding_with_tea
    Nov 15 '18 at 18:14



















  • thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

    – coding_with_tea
    Nov 15 '18 at 18:08











  • Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

    – Nunchy
    Nov 15 '18 at 18:09











  • it has the contents of a html header document inside

    – coding_with_tea
    Nov 15 '18 at 18:10











  • I see, but how are those contents being output? Can you post the file?

    – Nunchy
    Nov 15 '18 at 18:11






  • 1





    it's been solved by Damian Dziaduch above, thanks.

    – coding_with_tea
    Nov 15 '18 at 18:14

















thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

– coding_with_tea
Nov 15 '18 at 18:08





thanks, I've tried your code but it gives me a blank page, I have error reporting on but no errors are displayed.

– coding_with_tea
Nov 15 '18 at 18:08













Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

– Nunchy
Nov 15 '18 at 18:09





Hmmm, what's header.php actually doing? There's a few things that could be the cause...is the path being fed to require actualy valid?

– Nunchy
Nov 15 '18 at 18:09













it has the contents of a html header document inside

– coding_with_tea
Nov 15 '18 at 18:10





it has the contents of a html header document inside

– coding_with_tea
Nov 15 '18 at 18:10













I see, but how are those contents being output? Can you post the file?

– Nunchy
Nov 15 '18 at 18:11





I see, but how are those contents being output? Can you post the file?

– Nunchy
Nov 15 '18 at 18:11




1




1





it's been solved by Damian Dziaduch above, thanks.

– coding_with_tea
Nov 15 '18 at 18:14





it's been solved by Damian Dziaduch above, thanks.

– coding_with_tea
Nov 15 '18 at 18:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325300%2fdefine-a-directory-for-including-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine