8 Replies - 7108 Views - Last Post: 28 October 2011 - 08:29 AM Rate Topic: -----

#1 USIT-HI  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 14-September 11

define(__ROOT__, dirname(dirname(__FILE__)));

Posted 25 October 2011 - 08:01 AM

I'm trying to use REQUIRE_ONCE() for a files 4 or 5 levels below the index page of my web site.
The following, from the PHP Manual(http://us2.php.net/manual), is not clear to me.
-------------------------------------------------
Hints:
Use a combination of dirname(__FILE__) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files.

One of my typical example is:

<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php);
?> 

--------------------------------------------------
Do the double under lines (__) indicate methods or something else. '__ROOT__' in line 2, with its quotes, looks like a string but in line 3 it's not quoted??
My naive aattempt ...
  <?php 
  define(__ROOT__, dirname(dirname(__FILE__)));
  echo '__ROOT__', "<p>";
  $ROOT_File = require_once(__ROOT__.'/RGSTRTN_06_6.php');
  echo "$ROOT_File <p>";
  ?> 

produced an error: 'unexpected T_STRING' in line 2.

ANY HELP MUCH APPRECIATED
USIT-HI

This post has been edited by JackOfAllTrades: 25 October 2011 - 09:58 AM
Reason for edit:: Added code tags...AGAIN!!!


Is This A Good Question/Topic? 0
  • +

Replies To: define(__ROOT__, dirname(dirname(__FILE__)));

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 25 October 2011 - 09:28 AM

Hi,

define() is used to define a constant. The first parameter is the name of the constant and the second parameter is the value assigned to the constant.

__FILE__ is a "Magic Constant" that returns a path and file name of the file it is being called in.

dirname returns the parent directory of a file so:
 dirname(__FILE__);



Will return the parent directory of the file this code is being called in.

The nested dirnames refer to the practice of calling dirname multiple times to essentially move up the directory tree.

To get a better idea
 //this is the directory the file is in
 define('CURRENT_DIR',dirname(__FILE__));
 //this is the next directory up
 define('PARENT_DIR',dirname(dirname(__FILE__)));

 echo "Current directory : " . CURRENT_DIR . "<br/>";
 echo "Parent directory : " . PARENT_DIR . "<br />";
 //if you wanted to use this in a require
 require_once PARENT_DIR . "/RGSTRTN_06_6.php";



I believe your error is because you are not closing the string on line two:
require_once(__ROOT__.'/config.php);
//should be
require_once(__ROOT__.'/config.php');



Although it should be noted that require and include are not functions but rather language constructs they don't need parenthesis when called and can be done as I demonstrated.

Hope this helps :)

Edited to add quotes around first define parameter.

This post has been edited by Jstall: 25 October 2011 - 10:15 AM

Was This Post Helpful? 2
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 25 October 2011 - 09:46 AM

note on define(): the first parameter must be a string, i.e. define('CURRENT_DIR', dirname(__FILE__)); (otherwise a notice (undefined constant, assuming string) is emitted.

also note that constants, methods, etc. starting with a double underscore are supposed to be PHP internals, i.e. you should not define them yourself.
Was This Post Helpful? 3
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,486
  • Joined: 23-August 08

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 25 October 2011 - 09:59 AM

Og, help me, are you EVER going to even TRY to use code tags when you post code???

:code:
Was This Post Helpful? 1
  • +
  • -

#5 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 25 October 2011 - 10:13 AM

Whoops right you are , thanks for spotting that :)
Was This Post Helpful? 0
  • +
  • -

#6 USIT-HI  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 14-September 11

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 26 October 2011 - 09:26 AM

Thnx all for your useful information.

Jstall: demos were especially clarifying.

Dormilich:I recall reading that double underscores were for php internals. So I was surprised to see them used in the php manual in the subject example.

JackOfAllTrades: You can rest easier. Code tags are on the way. The first time you chastised me on the lack of their use, I looked up the term in my php reference and they showed them as <?php and ?>. So I sometimes dropped them thinking that they would be obvious in a PHP Forum. Then your example evidenced other tags. I think I did use them once but didn't, and still don't, understand what they do. I did watch the video.

 :bigsmile:/> 


USIT-HI
Was This Post Helpful? 0
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 26 October 2011 - 11:33 AM

The forum [ code] tags make the code you place in them easier to read. They also allow you to do things like copy a block of code to your clipboard or open the code in a new window etc.

When you don't use code tags moderators like JackOfAllTrades have to add them and each time they do that a little part of their soul dies. We can't have our moderators going around with no souls as it has been proven to seriously impact productivity.
Was This Post Helpful? 2
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,486
  • Joined: 23-August 08

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 26 October 2011 - 01:06 PM

You presuppose that I have a soul, Jstall! ;)

Thanks for figuring it out, USIT-HI. It just makes reading people's code much, much easier.
Was This Post Helpful? 1
  • +
  • -

#9 USIT-HI  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 14-September 11

Re: define(__ROOT__, dirname(dirname(__FILE__)));

Posted 28 October 2011 - 08:29 AM

got it -- a slow learner, but an appreciative one

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1