PHP Constants Constant arrays

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any PHP Question?