Home >Backend Development >PHP Problem >Which symbol in php can declare an array
The "[]" symbol in php can declare an array. Two declaration methods: 1. Use the "$array variable name = [element value]" statement. "[]" can contain multiple value lists, separated by commas. If the value list is omitted, an empty array is declared; 2 , use "$array variable name [subscript] = value" to directly declare the array and assign a value to the specified subscript element.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
"[ in php ]
" symbol can declare an array.
In PHP, square brackets "[]" are also called "array literals". There are two ways to use them to declare an array.
Method 1. Use “$array variable name=[element value]
”
"[]" does not contain a value, then an empty array is defined:
<?php header("Content-type:text/html;charset=utf-8"); $arr=[]; var_dump($arr); ?>
"[]" can also contain multiple value lists, with commas between values Separate;
<?php header("Content-type:text/html;charset=utf-8"); $arr=[1,2,3,4,5]; var_dump($arr); ?>
<?php header("Content-type:text/html;charset=utf-8"); $arr=[1=>"1","a"=>"red",2=>"2","b"=>"green","c"=>"blue"]; var_dump($arr); ?>
##Method 2, use "$array Variable name [subscript] = value ”
[ ].
<?php header("Content-type:text/html;charset=utf-8"); $array[0] = '欢迎'; $array[1] = '来到'; $array[2] = 'PHP中文网'; $array['url'] = 'https://www.php.cn/'; //输出语句 var_dump($array); ?>Output result: Example 2:
<?php header("Content-type:text/html;charset=utf-8"); $array[] = '香蕉'; $array[] = '苹果'; $array[] = '橘子'; $array[] = '榴莲'; //输出语句 var_dump($array); ?>Output result:
It can be seen that when we do not specify a specific index value within square brackets, the default is a numeric index, and the index value increases sequentially starting from 0 by default. Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of Which symbol in php can declare an array. For more information, please follow other related articles on the PHP Chinese website!