← Back to Chapters

PHP Constant Variables

? PHP Constant Variables

? Quick Overview

PHP constants store fixed values that cannot be changed once defined. They are ideal for configuration values that remain the same throughout script execution.

? Key Concepts

  • Constants are defined using define()
  • No $ symbol is used
  • Constants are globally accessible

? Syntax & Theory

? View Code Example
// Basic syntax for defining a constant in PHP
define(name, value, case_insensitive);

? Code Example

? View Code Example
// Defining and using a constant
<?php
define("SITE_NAME", "MyWebsite");
echo SITE_NAME;
?>

? Output

MyWebsite

? Case-Insensitive Constant

? View Code Example
// Example of a case-insensitive constant (deprecated)
<?php
define("GREETING", "Welcome!", true);
echo greeting;
?>

⚡ Interactive Lab: Constant Lockdown

Simulate how PHP handles constants. Try defining a constant and then try to change its value.

// Click run to see simulation results...

? Use Cases

  • Website configuration values
  • Application-wide settings
  • Fixed messages and limits

? Interactive Concept

Think of constants as locked labels in your application—once set, every part of your code can read them but never modify them.

✅ Tips & Best Practices

  • Use uppercase names for constants
  • Prefer constants for values that should not change
  • Avoid deprecated case-insensitive constants

? Try It Yourself

  • Create a PHP file named constant.php
  • Define a constant using define()
  • Echo it and try redefining it