PHP Magic Constants

0
2K

PHP Predefined Constants

PHP has nine predefined constants that change value depending on where they are used, and therefor they are called "magic constants".

These magic constants are written with a double underscore at the start and the end, except for the ClassName::class constant.


Magic Constants

Here are the magic constants, with descriptions and examples:

Constant Description  
__CLASS__ If used inside a class, the class name is returned. Try it »
__DIR__ The directory of the file. Try it »
__FILE__ The file name including the full path. Try it »
__FUNCTION__ If inside a function, the function name is returned. Try it »
__LINE__ The current line number. Try it »
__METHOD__ If used inside a function that belongs to a class, both class and function name is returned. Try it »
__NAMESPACE__ If used inside a namespace, the name of the namespace is returned. Try it »
__TRAIT__ If used inside a trait, the trait name is returned. Try it »
ClassName::class Returns the name of the specified class and the name of the namespace, if any. Try it »

Note:

The magic constants are case-insensitive, meaning __LINE__ returns the same as __line__.

Cerca
Categorie
Leggi tutto
Altre informazioni
PHP Variables Scope
PHP Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a...
By PHP Tutorial 2024-05-17 07:18:52 0 3K
Altre informazioni
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
By PHP Tutorial 2024-05-17 07:09:40 0 2K
Altre informazioni
PHP - Concatenate Strings
String Concatenation To concatenate, or combine, two strings you can use...
By PHP Tutorial 2024-05-17 07:30:24 0 3K
Altre informazioni
PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP. PHP Loops...
By PHP Tutorial 2024-05-17 07:48:35 0 2K
Altre informazioni
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
By PHP Tutorial 2024-05-17 07:45:23 0 2K