PHP Constant arrays


Example

Arrays can be used as plain constants and class constants from version PHP 5.6 onwards:

Class constant example

class Answer {
    const C = [2,4];
}

print Answer::C[1] . Answer::C[0]; // 42

Plain constant example

const ANSWER = [2,4];
print ANSWER[1] . ANSWER[0]; // 42

Also from version PHP 7.0 this functionality was ported to the define function for plain constants.

define('VALUES', [2, 3]);
define('MY_ARRAY', [
    1,
    VALUES,
]);

print MY_ARRAY[1][1]; // 3