PHP constant as an array

arrays, php

Solution

What you are looking for is called Constant array :

it is now possible to do this with define() but only for PHP7 . However you can do this on PHP5.6 by using the const keyword and it is not possible to perform this in lower PHP versions

here is an example :

<?php

define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // outputs "cat"

define('MYCONST', [
    'key' => "value"

    ]);

echo MYCONST['key']; // outputs "value"

Problem

Is there a possibility to make a group of constants and get them exactly like an array instead of writing each constant independently ? something like ``` echo MYCONST[0]; or echo MYCONST['name']; ```

Original source

Related problems