Lesson 8 of 50 – PHP Constants
16%

PHP Constants

In this lesson, you will learn what constants are in PHP, how to define them, and how to use them in your programs.

1. What Is a Constant in PHP?

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.

2. Defining Constants Using define()

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.

3. Defining Constants Using const

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.

4. Difference Between Variables and Constants
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.

5. Constant Naming Rules

Follow these rules when naming constants:

  • A constant name must start with a letter or underscore.
  • It can contain letters, numbers, and underscores.
  • It does not use the $ symbol.
  • Choose meaningful names for readability.
  • Uppercase names are a common convention, not a requirement.

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
6. Constants Are Case-Sensitive

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
?>
7. Using Constants in Calculations

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.

8. Checking Whether a Constant Exists

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.

9. Useful Predefined Constants

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.

📝 Practical Assignment

Complete these exercises using PHP constants:

  1. Create a constant named STUDENT_NAME with your name.
  2. Create a constant named COURSE_FEE with the value 10000.
  3. Create a constant named COLLEGE_NAME with your college name.
  4. Calculate the total cost of 5 books if BOOK_PRICE is 200.
  5. Use defined() to check whether a constant named WEBSITE exists.

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;
?>

🧠 Quick Quiz

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?