0

I'm new to PHP. I've been looking at the documentation and am having a problem. I have a multi-page PHP site I am working on. I was having a problem with relative paths (PHP Relative Path Issues) and was pointed to the following url(PHP include relative path). I want to use something similar to the following code snippet in the post:

if (is_production()) {
    define('ROOT_PATH', '/some/production/path');
}
else {
    define('ROOT_PATH', '/root');
}
include ROOT_PATH . '/connect.php';

On what page do I add the define statement (index.php?) and how can I reference the ROOT_PATH on every subsequent page that has an include statement?

I tried adding the define statement to the index.php page but calling the ROOT_PATH on any other page results in: Use of undefined constant ROOT_PATH

Index.php: define('ROOT_PATH', '/some/production/path');
some other page: include_once(ROOT_PATH."/Library/API/database.inc.php");

Community
  • 1
  • 1
FlyFish
  • 491
  • 5
  • 22
  • 3
    you create a config file for all environment variable and add this file to all of your php files @FlyFish – Parth Mahida Nov 10 '16 at 18:06
  • @RyanVincent I tried adding the define statement to the index.php but it wasn't being recognized on any subsequent pages. – FlyFish Nov 10 '16 at 18:29
  • To expand on @parthmahida ... create a file called something like config.php and in there, add all of your configs, constants, etc ... then on each page, include the config.php file at the top. – Spechal Nov 10 '16 at 18:42
  • @Spechal I'm stuck at the issue that started this. How do I reference the config file without using the complete absolute path Which is what I am trying to avoid in the first place. – FlyFish Nov 10 '16 at 19:01
  • I'm doing something wrong. I created a config.php file. I added in the following: return array('ROOT_PATH' => 'C:/Users/Me/Documents/app/site'); I added an include (include_once(ROOT_PATH."/Library/API/database.inc.php"); )to the file at the top of the page, but I get the following error: Notice: Use of undefined constant ROOT_PATH - assumed 'ROOT_PATH' – FlyFish Nov 10 '16 at 19:18

3 Answers3

0

You have 2 possible (common) approaches:

Create files for each environment like production.inc.php and development.inc.php and include the one you need in your index.php by using your is_production() condition.

-- OR --

Create a single file that contains

if (is_production()) {
    define('ROOT_PATH', '/some/production/path');
} else {
    define('ROOT_PATH', '/root');
}

and then just include that single file to all your pages that need those constants.

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • I think I wasn't clear. I'm only looking to have a SINGLE define statement. My issue is that I can't figure out how to populate the 'ROOT_PATH' constant and then access it on other pages. – FlyFish Nov 10 '16 at 18:58
  • @FlyFish that's what I meant with the last sentence on my answer. Just do `include_once("config.inc.php");` on all the pages that needs to access the contant. – dokgu Nov 10 '16 at 21:02
0

I believe my issue was a SCOPING Issue. Although I went with @ parthmahida and @Spechal suggestion, there was still an issue. I ended up having to look at scoping to see it in the functions.

FlyFish
  • 491
  • 5
  • 22
0

If you want to include a file that will contain your environment configuration information and constants, it will always have to be relative to the script calling it.

<?php

// config.php
function is_production(){ // do something to return boolean }
if (is_production()) {
    define('ROOT_PATH', '/some/production/path');
}
else {
    define('ROOT_PATH', '/root');
}

...

<?php

// index.php
include 'config.php';
include ROOT_PATH . 'some-file.php';
// do stuff

...

<?php

// some-page.php
include 'config.php';
include ROOT_PATH . 'other-file.php';
// do stuff
Spechal
  • 2,634
  • 6
  • 32
  • 48