Defining Case-insensitive Constants Has Been Removed in PHP 8.0

PHP allows to define constants using define function. Defined constants are case-sensitive. For example, DB_USER and db_user represents different constants.

In versions prior to PHP 8.0, it was possible to define case-insensitive constants by passing true as third parameter of the function. In PHP 7.3 and 7.4, the ability to define case-insensitive constants is deprecated.

<?php

define('DB_USER', 'root', true);

echo db_user;

Example will output:

Deprecated: define(): Declaration of case-insensitive constants is deprecated in main.php on line 3
Deprecated: Case-insensitive constants are deprecated. The correct casing for this constant is "DB_USER" in main.php on line 5
root

Since PHP 8.0, defining case-insensitive constants has been removed. The third parameter of the define function is ignored and using case-insensitive constant provides a fatal error.

If we run the previous code snippet in PHP 8.0 or newer versions, we will get a fatal error:

Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in main.php on line 3
Fatal error: Uncaught Error: Undefined constant "db_user" in main.php:5

We can define constants and use them as follows:

<?php

define('DB_USER', 'root');
define('db_user', 'root');

echo DB_USER;
echo db_user;

Leave a Comment

Your email address will not be published. Required fields are marked *