What is __DIR__?

Sometimes when I look at other peoples PHP code they have something like this in there:

include(DIR"file.php");

what is DIR?

Thanks.

The only one I know about is FILE, it is the full name and path of the current file.

The only way I can see that they get DIR is by defining it like this


define('__DIR__', pathinfo(__FILE__, PATHINFO_DIRNAME));

Which will give you the absolute path to the current file.

sorry yes i meant FILE

I dont quite get it - this refers to the filename of the PHP script or something else?

It refers to the absolute path and filename of the script that calls it. So, if I created a test script on my local machine, and say


echo __FILE__;

I would get this
C:\Apache2\htdocs\ est.php

If, say for example I had 2 scripts, called test1.php and test2.php, and I included test2.php in test1.php like this

test1.php


<?php
include('test2.php');
echo __FILE__;
?>

test2.php


<?php
echo __FILE__.'<br />';
?>

I would get this output
C:\Apache2\htdocs\ est2.php
C:\Apache2\htdocs\ est1.php

So it will always show the full path and filename of that script.

ok i see thanks. So its basically the same as $_SERVER[‘php_self’] except it shows the full pathname on the server.

You can also use this for the same purpose:


echo $_SERVER['SCRIPT_FILENAME'];

Yup, exactly right :smiley:

thanks, that is one mystery I can tick off the list :slight_smile:

Not exactly like PHP_SELF. That will always point to the file requested by the browser. If you want to know the path to an include, not to the file that includes it, you would use FILE.