PHP Magic Constants Documentation
__LINE__
The __LINE__
magic constant returns the current line number of the file where it is used. This is particularly useful for debugging, as it helps to pinpoint where an issue occurs within a file.
PHP Demo:
<?php
echo 'Current Line: ' . __LINE__;
?>
Output:
<?php
echo 'Current Line: ' . __LINE__;
?>
__FILE__
The __FILE__
magic constant returns the full path and filename of the file in which it is used. This is useful for debugging and including files, as it helps to track the exact location of the file being executed.
PHP Demo:
<?php
echo 'File Path: ' . __FILE__;
?>
Output:
<?php
echo 'File Path: ' . __FILE__;
?>
__DIR__
The __DIR__
magic constant returns the directory of the file where it is used. This is helpful for including files or referencing paths relative to the current directory.
PHP Demo:
<?php
echo 'Directory Path: ' . __DIR__;
?>
Output:
<?php
echo 'Directory Path: ' . __DIR__;
?>
__FUNCTION__
The __FUNCTION__
magic constant returns the name of the function in which it is used. If used outside of a function, it returns an empty string. This is useful for debugging and tracing function calls.
PHP Demo:
<?php
function demoFunction() {
echo 'Function Name: ' . __FUNCTION__;
}
demoFunction();
?>
Output:
<?php
function demoFunction() {
echo 'Function Name: ' . __FUNCTION__;
}
demoFunction();
?>