Constants in PHP are assigned values that cannot be changed during the script. A constant is by default a case sensitive and usually written with uppercase

Syntax for constants

define(name, value);

Constants in PHP

Figure: Example of PHP Constants

Magic Constant

Predefine constants that are stored in the PHP library and can be called upon request are called Magic Constants. Some of the magic constants of PHP are:

  • __LINE__ current line number of the file.
  • __FILE__ fetches the file path and file name.
  • __DIR__ brings the directory of the file.
  • __FUNCTION__ returns the function name.
  • __CLASS__ returns the class name


›› go to examples ››