How to get current directory, filename and code line number in PHP
PHP provides a large number of magical constants. All these constants are case sensitive. With the help of these constants, we can get the current directory, filename, current line number and much more. It is more secure, reliable, and fast, which has resulted in web developers preferring to work with PHP. So, it does not matter if the developer needs to know the line number or current directory of the PHP document.
To get the current directory
__DIR__: This constant is used to get the current directory of the file. This is equivalent to the dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. If used inside an include method, the directory of the included file is returned.
Output: The current directory of this file is 'C:\wamp\www\demo'.
To get the current filename with full file path
__FILE__: This is used to return the full file path and filename of the file. If used inside an include method, the name of the included file is returned.
Output: The current file with path is 'C:\wamp\www\demo\magic_const.php'.
This magic constant is mostly used in case where we have to include some files from the directory.
To get the current line number
__LINE__: This is used to return the current line number of the file.
Output: The line number of this code is '34'.
need an explanation for this answer? contact us directly to get an explanation for this answerThis constant is mostly used while debugging the code or to get line number of the error.