A Constant is an identifier (a name) for a simple value that cannot be changed during the execution of the script. Unlike variables, constants are automatically global and can be used anywhere in your script, regardless of scope.
1. Defining a Constant
You define a constant using the define() function.
| Syntax | Description |
define(name, value, case_insensitive) | Defines a constant. |
| Parameter | Purpose |
name | The name of the constant (e.g., "DB_SERVER"). |
value | The value of the constant (e.g., "localhost"). |
case_insensitive | Optional. Boolean (true or false). If true, the constant can be accessed using any case (e.g., db_server or DB_SERVER). Best practice is to use false or omit it. |
Naming Conventions
By convention, PHP constants should always be named in uppercase to easily distinguish them from variables.
Example: Defining a Database Constant
<?php
// 1. Define the constant (standard, case-sensitive)
define("DB_SERVER", "localhost");
define("SITE_TITLE", "My PHP Tutorials");
define("MAX_FILE_SIZE", 5000000); // 5MB limit
// 2. Access the constant (no $ sign!)
echo "Database Server: " . DB_SERVER . "<br>";
echo "Maximum Upload: " . MAX_FILE_SIZE . " bytes.";
// DB_SERVER = "127.0.0.1"; // ERROR: You cannot redefine a constant!
?>
2. Magic Constants
PHP provides several predefined constants that change their value based on where they are used in the script. They are called Magic Constants because they start and end with two underscores (__).
These are typically used for debugging, logging, and dynamic file path resolution.
| Constant | Description | Example Value |
__LINE__ | The current line number in the file. | 15 |
__FILE__ | The full path and filename of the file. | /var/www/html/index.php |
__DIR__ | The directory of the file (without the filename). | /var/www/html/ |
__FUNCTION__ | The name of the function currently being executed. | calculateTotal |
__CLASS__ | The name of the class currently being executed (in OOP). | UserHandler |
Example: Using Magic Constants for Debugging
<?php
function logError() {
// __FUNCTION__ returns "logError"
echo "Error occurred in function: " . __FUNCTION__ . "<br>";
// __FILE__ and __LINE__ pinpoint the exact file and location
echo "File: " . __FILE__ . " on line: " . __LINE__ . "<br>";
}
logError();
// Output:
// Error occurred in function: logError
// File: /path/to/script.php on line: 5
?>
3. Checking if a Constant Exists
Before defining a constant, it is often good practice to check if it has already been defined, particularly in large applications where configuration files might be loaded multiple times.
You use the defined() function for this check.
Example:
<?php
if (!defined("APP_VERSION")) {
define("APP_VERSION", "1.0.1");
echo "Version constant set.";
} else {
echo "Version constant already exists.";
}
?>
⏭️ Next Steps
You have now covered all the essential elements of procedural PHP: variables, logic, data types, strings, numbers, arrays, dates, and constants.
The next necessary chapter should cover Regular Expressions (RegEx), the powerful tool used for advanced form validation and text parsing.
