- PHP 5.6 中 仅能通过 const 定义常量数组
- PHP 7 中 define 也可以定义常量数组了
测试一:不在类中使用
<?php// define 定义常量数组define('info', ['name' => '向上','age' => 25,'status' => 'poo1']);print_r(info);print(PHP_EOL);print_r(info['name']);print(PHP_EOL);echo '<hr>';const color = ['deepRed', 'green', 'blue'];print_r(color);
运算结果:
测试二:在类中使用
<?php// 不出所料 define 还是不能够在类中使用,还是const靠谱。class Test{const info = ['xs', 'age', 25];public static function getInfo(){print_r(self::info);}}Test::getInfo(); // Array ( [0] => xs [1] => age [2] => 25 )
