PHP: Magic constants

Apart from regular constants, PHP also has a separate constant type - magic constants. We can sum up their difference as follows:

  • Magic constants can't be defined by us, we only can use the exsisting ones.
  • Magic constant is wrapped in double underscores __.
  • The magic is that such constants have the same value only when used in the same part of the program.

The last point may sound weird to you. It doesn't sound like a constant, right? They may not be permanent, but the way they change is strictly regulated and it doesn't cause us any troubles. Examples of such constants are:

  • __LINE__ — contains the current line of the file in which this constant is used
  • __FILE__ — path to the current file
  • __DIR__ — path to the directory of the current file


Output the path to the current directory using a magic constant.



  • Magic constant - a special constant predefined in the PHP core; it's value depends on the context

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community