In this lesson, you will learn what constants are in PHP, how to define them, and how to use them in your programs.
A constant is a name that represents a value which cannot be changed after it has been defined.
Unlike variables, constants do not begin with the dollar sign ($).
<?php
define("SITE_NAME", "Soopro Pathshala");
echo SITE_NAME;
?>
Output:
Soopro Pathshala
Here, SITE_NAME is a constant whose value
is Soopro Pathshala.
The define() function creates a constant
at runtime.
Syntax:
define("CONSTANT_NAME", value);
Example:
<?php
define("COLLEGE_NAME", "ABC College");
define("COURSE_FEE", 10000);
echo COLLEGE_NAME;
echo "<br>";
echo COURSE_FEE;
?>
Output:
ABC College
10000
The first argument is the constant name, and the second argument is its value.
PHP also provides the const keyword
to declare constants.
<?php
const PI = 3.14159;
const COUNTRY = "India";
echo PI;
echo "<br>";
echo COUNTRY;
?>
Output:
3.14159
India
Use const for constants declared in
supported declaration contexts, such as at the
top level of a PHP file or inside a class.
Unlike define(), it cannot be used
as an ordinary runtime statement inside a function.
| Variables | Constants |
|---|---|
| Begin with $ | Do not begin with $ |
| Values can be reassigned | Values cannot be reassigned |
| Example: $price | Example: PRICE |
| Created using assignment | Created using define() or const |
Example:
<?php
$price = 500;
$price = 700;
define("TAX_RATE", 18);
echo $price;
echo "<br>";
echo TAX_RATE;
?>
Output:
700
18
The variable value changes from 500 to 700, while the constant remains 18.
Follow these rules when naming constants:
Valid examples:
<?php
define("APP_NAME", "Soopro");
define("MAX_STUDENTS", 100);
define("_VERSION", "1.0");
echo APP_NAME;
echo "<br>";
echo MAX_STUDENTS;
echo "<br>";
echo _VERSION;
?>
Output:
Soopro
100
1.0
Constants defined with define() are
case-sensitive by default. Constants declared with
const are also case-sensitive.
<?php
define("CITY", "Aurangabad");
echo CITY;
?>
Output:
Aurangabad
CITY and city are different
constant names. Using an undefined constant name
without quotes will generally produce an error in
modern PHP.
Example:
<?php
define("CITY", "Aurangabad");
echo CITY;
// echo city; // Undefined constant error
?>
Constants can be used in calculations and expressions.
<?php
define("PRICE", 500);
define("QUANTITY", 4);
$total = PRICE * QUANTITY;
echo "Total Price: " . $total;
?>
Output:
Total Price: 2000
Here, PHP multiplies the constant values and stores
the result in the variable $total.
The defined() function checks whether
a constant has already been defined.
<?php
define("APP_VERSION", "2.0");
if (defined("APP_VERSION")) {
echo "Constant exists";
} else {
echo "Constant does not exist";
}
?>
Output:
Constant exists
The function returns true if the
constant exists and false otherwise.
Pass the constant name as a string.
PHP provides predefined constants that give information about the current script.
| Constant | Purpose |
|---|---|
PHP_VERSION |
Returns the PHP version. |
PHP_OS |
Identifies the operating system PHP was built for. |
PHP_INT_MAX |
Returns the maximum integer supported by this build. |
PHP_EOL |
Provides the platform-specific line-ending sequence. |
Example:
<?php
echo "PHP Version: " . PHP_VERSION;
echo "<br>";
echo "Operating System: " . PHP_OS;
echo "<br>";
echo "Maximum Integer: " . PHP_INT_MAX;
?>
The output depends on the PHP version and environment where the program runs.
Complete these exercises using PHP constants:
Sample starting code:
<?php
define("STUDENT_NAME", "Rahul");
define("COURSE_FEE", 10000);
define("BOOK_PRICE", 200);
$totalBooksCost = BOOK_PRICE * 5;
echo STUDENT_NAME;
echo "<br>";
echo COURSE_FEE;
echo "<br>";
echo $totalBooksCost;
?>
1. Which function is used to define a constant at runtime?
2. Which symbol is used at the beginning of a PHP constant name?
3. Which function checks whether a constant exists?