Research Documentation 02 Magic Constants

Research Documentation 02 Magic Constants

02 - Magic Constants

__LINE__

The __LINE__ magic constant in PHP returns the current line number of the file. The syntax for using __LINE__ could look something like this:

<?php echo "The current Line number is " . __LINE__ . "."; ?>

When this is ran, it produces this output:

The current Line number is 11.

__FILE__

The __FILE__ magic constant in PHP returns the name of the full path and filename of the file. If it is used inside an include, the name of the included file is returned. An example of this could look like:

<?php echo "The name of this file path is " . __FILE__ . "."; ?>

When it is ran, it produces this output:

The name of this file path is /var/www/vhosts/adetienne.bitweb1.nwtc.edu/httpdocs/php/research-documentation/02-magic-constants.php.

__DIR__

The __DIR__ magic constant in PHP returns the directory of a file. If it is used inside an include, the directory of the included file is returned. An example of what this could look like:

<?php echo "The directory of this current PHP script is " . __DIR__ "."; ?>

When it is ran it produces this output:

The directory of this current PHP script is /var/www/vhosts/adetienne.bitweb1.nwtc.edu/httpdocs/php/research-documentation.

_FUNCTION_

The __FUNCTION__ magic constant in PHP returns the name of the function the constant is used in. An example of this includes:

<?php function testFunction() {
         echo "The current function name is " . __FUNCTION__ . ".";
        }
        testFunction(); 
        ?>

When it is ran it produces this output:

The current function name is testFunction.

Resources

In summary, there are nine magic constants in PHP that can change depending on where they are used. Magic constants are resolved at compile time rather than runtime like regular constants. The constants are case-insensitive. Some resources to check out about magic constants and to learn more about the other ones, check out the links for these resources below: