Categories
Code Snippets PHP

How to get current directory, filename and code line number in PHP

How to Get the Current Directory, Filename, and Code Line Number in PHP

In PHP, there are several built-in functions that can be used to obtain information about the current script’s directory, filename, and code line number.

Getting the Current Directory

To get the absolute path of the directory containing the current script, you can use the dirname(__FILE__) function. For example:

$current_directory = dirname(__FILE__);

This will set the $current_directory variable to the absolute path of the directory containing the current script.

Getting the Current Filename

To get the name of the current script file, you can use the basename(__FILE__) function. For example:

$current_filename = basename(__FILE__);

This will set the $current_filename variable to the name of the current script file.

Getting the Current Code Line Number

To get the current code line number, you can use the __LINE__ magic constant. For example:

$current_line_number = __LINE__;

This will set the $current_line_number variable to the current code line number.

By using these built-in functions and magic constants, you can easily obtain information about the current script’s directory, filename, and code line number in PHP.

One reply on “How to get current directory, filename and code line number in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *