title: Constant defined by PHP define() really global?
body: <p>I just define a root path as a constant like this</p>
  
  <pre><code>define("ROOT_PATH", dirname(__FILE__));
  </code></pre>
  
  <p>so, at the same file, they can recognize ROOT_PATH, however, in the other file, ROOT_PATH is null, I check some forums, people said defined constant can be use in global, however, I can't, is that I need to do some config in PHP.ini?</p>
  
  <p>Actually, why I do that is because I'm moving the site to new server, however, the new server seems don't like me use relative path, therefore, I need to change to absolute path.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Thanks everyone, I think I catch the reason, I use ajax to call the file, so the ROOT_PATH is null, therefore, I define ROOT_PATH in every file, but another problem is appear, my file structure is sth like this</p>
  
  <pre><code>/index.php
  /ajax/a1.php
  /inc/inc.php
  
  index.php:
  &lt;?
  define('ROOT_PATH',dirname(__FILE__));
  include_once ROOT_PATH."/inc/inc.php";
  ?&gt;
  
  
  a1.php:
  &lt;?
  define('ROOT_PATH',dirname(__FILE__));
  include_once ROOT_PATH."/inc/inc.php";
  ?&gt;
  </code></pre>
  
  <p>so, when the client side call a1.php, it will include inc.php, however, it doesn't like what I imagine, I get this from the chrome developer tools</p>
  
  <pre><code>&lt;b&gt;Warning&lt;/b&gt;:  include_once(D:\.............\ajax/inc/a1.php) [&lt;a href='function.include-once'&gt;function.include-once&lt;/a&gt;]: failed to open stream: No such file or directory in &lt;b&gt;D:\..................\inc\inc.php&lt;/b&gt; on line &lt;b&gt;3&lt;/b&gt;&lt;br /&gt;
  </code></pre>
  
  <p>I check with the ROOT_PATH generated by both file, find that, <strong>FILE</strong> is depends on the caller, </p>
  
  <p>if index.php call it, the </p>
  
  <pre><code>ROOT_PATH = d:\.............\
  </code></pre>
  
  <p>if a1.php call it, the </p>
  
  <pre><code>ROOT_PATH = d:\..............\ajax\
  </code></pre>
  
  <p>however, both file I need to call that file, any method can I overcome this? thanks.</p>
  
  <p>SOLVED:</p>
  
  <p>Hi, I just figure out how to over come the path problem, first of all, we need to setup a file in every directory, and this file store the absolute path information refer to the relative path(hard to understand? :P), I call this file <code>path.php</code>, for my example</p>
  
  <pre><code>/index.php
  /ajax/a1.php
  /inc/inc.php
  </code></pre>
  
  <p>therefore, we need to add 3 path.php in this 3 directroy, as follow,</p>
  
  <pre><code>/index.php
  /path.php
  /ajax/a1.php
  /ajax/path.php
  /inc/inc.php
  /inc/path.php
  </code></pre>
  
  <p>each setting of the <code>path.php</code> like this,</p>
  
  <p><code>/path.php</code></p>
  
  <pre><code> &lt;?
  
  if(!defined(ROOT_PATH))
      define(ROOT_PATH, dirname(__FILE__));
  
  ?&gt;
  </code></pre>
  
  <p><code>/ajax/path.php</code> &amp; <code>/inc/path.php</code> </p>
  
  <pre><code>&lt;?
  
  if(!defined(ROOT_PATH))
      define(ROOT_PATH, '..');
  
  ?&gt;
  </code></pre>
  
  <p>so, if we have a 3 leve structure, let say: <code>/one/two/</code>, then the <code>/one/two/path.php</code> should be like this</p>
  
  <pre><code>&lt;?
  
  if(!defined(ROOT_PATH))
      define(ROOT_PATH, '../..');
  
  ?&gt;
  </code></pre>
  
  <p>After we set up all this file, we need to include this <code>path.php</code> as follow, for example with <code>index.php</code> </p>
  
  <p><code>/index.php</code></p>
  
  <pre><code>&lt;?
  
  include_once "path.php";
  include_once ROOT_PATH."/inc/inc.php";
  
  ...
  ?&gt;
  </code></pre>
  
  <p>So, whoever the caller is, it will first include the local <code>path.php</code>, and find out the relative path <code>(/ or .. or ../..)</code>, therefore, the included file will not be wrong because all of them are include with correct symbol, let say, </p>
  
  <p>if caller is /index.php, then ROOT_PATH = "FULLPATH", so</p>
  
  <pre><code>include_once ROOT_PATH."/inc/inc.php";
  =
  include_once "FULLPATH"."/inc/inc.php";
  </code></pre>
  
  <p>if caller is /ajax/a1.php, then ROOT_PATH = "..", so</p>
  
  <pre><code>include_once ROOT_PATH."/inc/inc.php";
  =
  include_once ".."."/inc/inc.php";
  </code></pre>