In PHP, __DIR__ is a predefined constant that returns the directory of the current file and is a useful tool for locating and accessing files and directories relative to the current file. This article, will discuss and explain how to use __DIR__ in PHP and some common use cases.
How to set __DIR__ in PHP
The __DIR__ constant returns the directory of the current file as an absolute path, as it means that it will always return the full path to the directory, regardless of the location of the script that is using it. Here’s an example of how to use __DIR__:
echo $directory;
In the code above, we have assigned the value of __DIR__ to a variable called $directory. We then output the value of the $directory using the echo statement. This will display the full path to the directory of the current file:
How to Use __DIR__ with include
One of the most common uses of __DIR__ is for including files. Let’s say we have a file called functions.php that has several useful functions that we wish to incorporate into our main script. We can use __DIR__ to include the file relative to the current file’s directory, like this:
In the code above, we’ve used the include statement to include the functions.php file. We’ve used __DIR__ to specify the directory of the current file, and then appended /functions.php to the end of it to specify the path to the file we want to include. Here is an illustration of how the functions.php file code might look like:
function addNumbers($a, $b) {
return $a + $b;
}
?>
You might use __DIR__ to include this file in your index.php file as it only requires two arguments and returns their sum.
include __DIR__ . '/functions.php';
$sum = addNumbers(5, 7);
echo "The sum of 5 and 7 is " . $sum;
?>
This code will include the functions.php file using __DIR__ and then call the addNumbers function to calculate the sum of 5 and 7. The output of the script will be “The sum of 5 and 7 is 12”.
How to Use __DIR__ with require
Similarly, we can use __DIR__ with the require statement to include required files in PHP, the syntax is the same as for the include statement:
This will need the functions.php file, and if it cannot be located, a fatal error will be generated, preventing the script from functioning. The functions.php file contains a function called greet() that prints a greeting message:
function greet($name) {
echo "Hello, $name!";
}
?>
Now, in your index.php file, include the functions.php file so that you can use the greet() function:
require __DIR__ . '/functions.php';
greet('SAM');
?>
In this example, the require statement includes the functions.php. The greeting “Hello, SAM!” is printed to the screen when the welcome() method is called with the input “SAM”:
Note that the require statement will throw a fatal error if the specified file is not found or cannot be included.
Conclusion
In this article, we’ve gone over how to use the __DIR__ constant in PHP to get the path of the current file. We’ve also covered how to use __DIR__ for including files using both include and require statements. Using __DIR__ can make it much easier to write code that works across different environments and file structures.