Open In App

PHP Constants

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A constant is a name or identifier used to store a fixed value that does not change during the execution of a PHP script. Unlike variables, constants do not start with a $ symbol and stay the same once they are defined.

  • Constants are immutable (cannot be changed after definition).
  • They are global by default and accessible from anywhere in the script.
  • Constants do not start with a $ symbol.
  • Constants are written in uppercase letters by convention.

Creating a Constant in PHP

There are two ways to create constants in PHP:

1. Using define() Function

The define() function in PHP is used to create a constant, as shown below:

Syntax

define( name, value);

The parameters are as follows:

  • name: The name of the constant.
  • value: The value to be stored in the constant.

Now, let us understand with the help of the example:

PHP
<?php
// This creates a case-sensitive constant
define("WELCOME", "GeeksforGeeks");
echo WELCOME . "\n";
// This creates a case-insensitive constant
define("HELLO", "GeeksforGeeks", true);
echo hello;
?>

Output
GeeksforGeeks
GeeksforGeeks

2. Using the Const Keyword

The const keyword is another way to define constants but is typically used inside classes and functions. The key difference from define() is that constants defined using const cannot be case-insensitive.

Syntax

const CONSTANT_NAME = value;

Now, let us understand with the help of the example:

PHP
<?php
const SITE_NAME = 'GeeksforGeeks';
echo SITE_NAME;
?>

Output
GeeksforGeeks

define() vs const

define()

const

Used to create constants using a function.

Used to create constants using a keyword.

Works at runtime.

Works at compile time.

Slightly older and more flexible.

Preferred in modern PHP code for structure and readability.

Constants are Global

By default, constants are global and can be used throughout the script, accessible inside and outside of any function.

Now, let us understand with the help of the example:

PHP
<?php
define("WELCOME", "GeeksforGeeks");
function testGlobal() {
    echo WELCOME;
}
echo WELCOME . "\n";
testGlobal();
?>

Output
GeeksforGeeks
GeeksforGeeks

Predefined Constants in PHP

PHP provides many built-in constants, such as:

  • PHP_VERSION: Current PHP version
  • PHP_OS: Operating system
  • PHP_INT_MAX: Maximum integer value etc.

Best Practices for Using Constants

  • Use UPPERCASE letters and underscores for naming.
  • Group related constants into classes using const.
  • Use constants for configuration settings, file paths, limits, etc.

Constants vs Variables

Both are used to store values in PHP. Constants hold fixed values that never change, whereas variables can be updated as the script runs. Below is the difference between them:

Constant

Variable

Used to store values that do not change during script execution.

Used to store values that can change while the script runs.

Defined once and cannot be redefined.

Can be updated or reassigned multiple times.

Do not start with a $ symbol.

Always start with a $ symbol.

Value stays the same everywhere in the program.

Value can be different in different parts of the program.

Conclusion

Constants in PHP are essential for storing fixed values that remain the same throughout the execution of a script. They help make your code more readable, maintainable, and error-free by preventing accidental value changes. By using constants wisely, especially for configuration settings, limits, and fixed labels—you can write cleaner and more reliable PHP programs. Understanding the difference between constants and variables also ensures you choose the right one based on the behavior your code requires.


Next Article

Similar Reads