This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
2.7 PHP Magic Constants
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__.
Animated PHP icons used in the buttons provided by ICONS8.COM.
Smartphone icons created by Freepik - Flaticon
Module 2 quiz
Example files created in this module:
PHP String
PHP Integer
PHP Float
PHP Array
PHP Object
PHP NULL Value
PHP String Functions - strlen() Return the Length of a String
PHP String Functions - str_word_count() Count Words in a String
PHP String Functions - strrev() Reverse a String
PHP String Functions - strpos() Search For a Text Within a String
PHP String Functions - str_replace() Replace Text Within a String
PHP Numbers - Integers is_int()
PHP Numbers - Floats is_float()
PHP Numbers - Check if a numeric value is finite
PHP Numbers - Invalid calculation will return a NaN value:
PHP Numbers - Check if the variable is numeric
PHP Numbers - Cast float to int
PHP Constants - Create a PHP Constant part 1
PHP Constants - Create a PHP Constant part 2
PHP Constants - Create an Array constant:
PHP Constants - Constants are Global
PHP Casting - Cast to String
PHP Casting - Cast to Integer
PHP Casting - Cast to Float
PHP Casting - Cast to Boolean
PHP Casting - Cast to Array part 1
PHP Casting - Cast to Array part 2
PHP Casting - Cast to Object part 1
PHP Casting - Cast to Object part 2
PHP Casting - Cast to NULL