PHP Magic Constants

PHP Magic Constants

PHP provides a large number of predefined constants to any script which it runs.

However, there are nine "magical" constants that change depending on where they are used. These are called Magic Constants. They usually start and end with double underscores (__).


Common Magic Constants

Here are the most widely used magic constants in professional PHP development:

Example Usage

Magic constants are incredibly useful for debugging or dynamically including files based on relative folder paths.

// Includes a file safely using absolute paths
require_once __DIR__ . '/includes/header.php';