# 常量数组

从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"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php7.shujuwajue.com/php-70x-xin-te-xing/qi-ta-te-xing-he-bian-geng/chang-liang-shu-zu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
