PHP Constants

PHP Tutorial

PHP Constants – Complete Beginner Guide With Examples

What Are Constants in PHP?

In PHP, a constant is a name (identifier) for a simple value that cannot be changed during script execution.

Unlike variables, constants:

  • Do not use the $ symbol

  • Cannot be redefined

  • Cannot change their value once set

Constants are commonly used for:

  • Configuration settings

  • Database credentials

  • Fixed values like tax rates

  • Application settings

Example:

Output:

Code Capsule

Difference Between Variables and Constants

FeatureVariableConstant
SymbolUses $No $
Value ChangeCan changeCannot change
ScopeDependsGlobal by default
Declaration$x = 10;define() or const

Constants are best for fixed values.


How to Create Constants in PHP

There are two main ways to create constants:

  1. Using define() function

  2. Using const keyword

Let’s understand both.


Using define() Function

The define() function is used to create constants.

Syntax:

Example:

Output:

3.14

Important:

  • Constant names are usually written in uppercase (best practice).

  • Names are case-sensitive by default.


 Using const Keyword

The const keyword can also define constants.

Example:


Difference Between define() and const

Featuredefine()const
Works Outside ClassYesYes
Works Inside ClassYesYes
Can Use in ConditionsYesNo
Compile-Time ConstantNoYes

For simple scripts, both work fine.


Constant Naming Rules

When creating constants:

  •  Must start with a letter or underscore
  •  Cannot start with a number
  •  No $ symbol
  •  Case-sensitive (by default)

Valid:

Invalid:


Accessing Constants

Unlike variables, constants are accessed directly by name.

No $ required.


Constants Are Global

Constants are automatically global.

Example:


 

Output:

Hello World

No need to use global.


Magic Constants in PHP

PHP provides built-in predefined constants called Magic Constants.

They change depending on where they are used.


LINE

Returns current line number.


FILE

Returns full file path.


DIR

Returns directory of file.


FUNCTION

Returns function name.


CLASS

Returns class name.


METHOD

Returns class method name.


Predefined PHP Constants

PHP provides many built-in constants.

Examples:

  • PHP_VERSION

  • PHP_OS

  • PHP_INT_MAX

  • E_ERROR

  • TRUE

  • FALSE

Example:

Displays installed PHP version.


Constants in Classes

You can define constants inside classes.

Example:


 

Access using:


Real-World Example – Database Configuration

Constants are commonly used for configuration.

This prevents accidental modification.


Why Use Constants?

Constants are useful because:

  •  They protect important values
  •  Improve code readability
  •  Reduce accidental changes
  •  Ideal for configuration settings
  •  Automatically global

Can Constants Store Arrays?

Yes (in modern PHP versions).

Or using const:


Checking if Constant Exists

Use defined().


Common Beginner Mistakes

 Using $ with Constants

Wrong:

Correct:


 Trying to Redefine Constant

This will cause an error.


 Using Lowercase Constant Names

Best practice is uppercase:

SITE_NAME

Best Practices for Using Constants

  •  Use uppercase naming
  •  Use constants for fixed values
  •  Use for configuration settings
  •  Avoid redefining
  •  Use descriptive names

Variable vs Constant Example

Use variable when value may change.

Use constant when value must remain fixed.


Why PHP Constants Are Important

Constants are essential for:

  • Application settings

  • Security configuration

  • Database credentials

  • API keys

  • Fixed rates and values

Without constants, maintaining large applications becomes difficult.


FAQs About PHP Constants

1. What is a constant in PHP?

A constant is a fixed value that cannot be changed once defined.


2. How do you define a constant in PHP?

Using define() function or const keyword.


3. Can constants be changed?

No, constants cannot be modified after definition.


4. What is the difference between const and define()?

const is a compile-time constant, while define() is a runtime function.


5. Are PHP constants global?

Yes, constants are automatically global.


Conclusion

PHP constants are essential for storing fixed values that should not change during program execution.

By understanding:

  • How to define constants

  • Difference between define() and const

  • Magic constants

  • Class constants

  • Best practices

You can write safer, more maintainable PHP applications.

Constants are especially useful for configuration settings and fixed values.

You may also like...