> For the complete documentation index, see [llms.txt](https://php7.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php7.shujuwajue.com/php-70x-xin-te-xing/qi-ta-te-xing-he-bian-geng/chang-liang-shu-zu.md).

# 常量数组

从PHP5.6开始，常量数组可以使用const关键词来声明，代码如下：

```php
const ANIMALS = ['dog', 'cat', 'bird'];
```

与普通数组做一个对比,代码如下:

```php
const ANIMALS = ['aa' => 'dog', 'cat', 'bird'];
echo ANIMALS[0] . '<br>';
echo ANIMALS['aa'] . '<br>';

$amimals =  ['aa' => 'dog', 'cat', 'bird'];
echo $amimals[0] . '<br>';
echo $amimals['aa'] . '<br>';
```

输出结果:

```
cat
dog
cat
dog
```

在PHP7中常量数组也可以通过define函数来初始化。代码如下：

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

echo ANIMALS[1]; // 输出 "cat"
```

补充

```php
define('ANIMALS', [
    'aa' => 'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // 输出 "bird"
echo ANIMALS['aa']; // 输出 "dog"
```
