PHP Constants
What are Constants?
Section titled “What are Constants?”Constants are like variables, but their value cannot be changed once defined. They’re useful for values that should remain fixed throughout your application, like configuration settings.
define('SITE_NAME', 'My Website');echo SITE_NAME; // My Website
// SITE_NAME = 'Other'; // Error: cannot reassign a constantDefining Constants
Section titled “Defining Constants”Using define()
Section titled “Using define()”Works anywhere in your code, including inside functions and conditionals.
define('MAX_USERS', 1000);define('TAX_RATE', 0.085);define('DEBUG_MODE', true);define('ALLOWED_TYPES', ['jpg', 'png', 'gif']);Using const
Section titled “Using const”Must be declared at the top level of a script or inside a class. Slightly faster performance.
const MAX_USERS = 1000;const TAX_RATE = 0.085;const DEBUG_MODE = true;const ALLOWED_TYPES = ['jpg', 'png', 'gif'];Key Differences
Section titled “Key Differences”| Feature | define() | const |
|---|---|---|
| Location | Anywhere (functions, conditionals) | Top level or class only |
| Name | Can use variables | Must be literal string |
| Speed | Slightly slower | Slightly faster |
Checking and Accessing Constants
Section titled “Checking and Accessing Constants”// Check if definedif (defined('MAX_USERS')) { echo MAX_USERS;}
// Access array constant elementsecho ALLOWED_TYPES[0]; // jpgClass Constants
Section titled “Class Constants”Constants can be defined inside classes and accessed with ::.
class AppConfig { const VERSION = '2.1.0'; const MAX_FILE_SIZE = 5242880; const SUPPORTED_FORMATS = ['json', 'xml', 'csv'];}
echo AppConfig::VERSION; // 2.1.0echo AppConfig::SUPPORTED_FORMATS[0]; // jsonMagic Constants
Section titled “Magic Constants”Magic constants are predefined by PHP and provide information about the current script. They start and end with double underscores and their values change based on where they’re used.
__LINE__
Section titled “__LINE__”Current line number.
echo __LINE__; // 5__FILE__
Section titled “__FILE__”Full path and filename.
echo __FILE__; // /var/www/html/index.phpecho basename(__FILE__); // index.php__DIR__
Section titled “__DIR__”Directory of the current file.
echo __DIR__; // /var/www/htmlinclude __DIR__ . '/config.php';__FUNCTION__
Section titled “__FUNCTION__”Current function name.
function process() { echo __FUNCTION__; // process}__CLASS__
Section titled “__CLASS__”Current class name.
class User { public function getName() { echo __CLASS__; // User }}__METHOD__
Section titled “__METHOD__”Current class and method name.
class User { public function save() { echo __METHOD__; // User::save }}__NAMESPACE__
Section titled “__NAMESPACE__”Current namespace.
namespace App\Controllers;
echo __NAMESPACE__; // App\Controllers__TRAIT__
Section titled “__TRAIT__”Current trait name.
trait Loggable { public function log() { echo __TRAIT__; // Loggable }}Practical Example
Section titled “Practical Example”Magic constants are useful for debugging and logging:
function logError($message) { $entry = sprintf( "[%s] %s:%d - %s", date('Y-m-d H:i:s'), __FILE__, __LINE__, $message ); error_log($entry);}