📦 PHP Arrays
Estimated reading: 3 minutes 279 views

PHP Constant Arrays – Define Immutable Array Data in PHP

Learn how to define and use constant arrays in PHP using define() and const for secure, unchangeable values.


Introduction – Why Use Constant Arrays?

Constant arrays in PHP are immutable collections of values that cannot be changed after declaration. They’re great for defining fixed data, like configuration settings, status codes, or predefined lists, ensuring consistency and preventing accidental modifications.

In this guide, you’ll learn:

  • How to define constant arrays using define() and const
  • When to use each method
  • Accessing values in constant arrays
  • Best practices for using constants effectively

1. Defining Constant Arrays Using define()

Introduced in PHP 7.0, define() can now create array constants.

define('FRUITS', ['apple', 'banana', 'cherry']);

echo FRUITS[0]; // apple

define() creates a global constant accessible throughout the script.


2. Defining Constant Arrays with const

Use const inside classes or in global scope (but only with fixed keys/values known at compile time).

const COLORS = ['red', 'green', 'blue'];

echo COLORS[1]; // green

Unlike define(), const is compile-time only and cannot be used conditionally.


3. Class Constant Arrays

class Config {
    public const ROLES = ['admin', 'editor', 'viewer'];
}

echo Config::ROLES[2]; // viewer

Ideal for namespacing constants logically inside a class.


Example – Constant Error Codes

define('ERROR_CODES', [
    '404' => 'Not Found',
    '500' => 'Internal Server Error',
    '403' => 'Forbidden'
]);

echo ERROR_CODES['404']; // Not Found

Used in APIs or systems where messages must stay static.


Looping Through Constant Arrays

foreach (FRUITS as $fruit) {
    echo $fruit . "<br>";
}

Constant arrays behave just like regular arrays for read-only operations.


Attempting to Modify a Constant Array

FRUITS[0] = 'orange'; //  Fatal Error

Constants are read-only. Any attempt to modify them will trigger a runtime error.


define() vs const Comparison

Featuredefine()const
Available sincePHP 7.0 (arrays)PHP 5.6
Works in class? No Yes
ScopeGlobalLocal (class or global)
Conditional usage Allowed Not allowed
Compile-time required No Yes

Best Practices

  • Use const inside classes for structured constant groups
  • Use uppercase names for all constants (e.g., STATUS_CODES)
  • Avoid modifying constant arrays — they are immutable
  • Do not use const inside functions (use define() instead)
  • Group related constants in classes or configuration files

Summary – Recap & Next Steps

Constant arrays in PHP help define immutable, secure values that won’t change during execution. They enforce cleaner code, minimize bugs, and enhance reliability in application configuration and business logic.

Key Takeaways:

  • Use define() or const to declare read-only arrays
  • Best for roles, statuses, config, and error messages
  • Cannot be changed or unset
  • Can be accessed globally (define) or within class (const)

Real-World Use Cases:
App-wide config arrays, static permissions, error/message maps, enum-like structures.


Frequently Asked Questions (FAQs)

Can I change a constant array after defining it?
No. Constant arrays are immutable by design.

What PHP version supports array constants with define()?
PHP 7.0 and above.

Can I use const in a function?
No. Use define() if you need to declare constants in conditional blocks or functions.

Are constant arrays secure for global configuration?
Yes. They’re safe, efficient, and can’t be accidentally modified.


Share Now :
Share

🛡️ PHP Constant Arrays

Or Copy Link

CONTENTS
Scroll to Top