Home >Backend Development >PHP Tutorial >What is the commonly used function to define arrays in php?
In PHP, two common functions for defining arrays are: array() function: generates an indexed array with keys as numbers. [] Short syntax: Generate an indexed array with keys in order starting from 0.
Common functions to define arrays in PHP
In PHP, there are two common methods to define arrays:
<code class="php">$array = array(1, 2, 3, 4, 5);</code>
This syntax was introduced in PHP 5.4 and higher.
<code class="php">$array = [1, 2, 3, 4, 5];</code>
Detailed description:
Which function to choose?
#Use the array() function: When array keys need to be specified explicitly. For example:
<code class="php">$array = array("key1" => 1, "key2" => 2, "key3" => 3);</code>
Use [] short syntax: When you need to create an index array, the keys of the array are numbers and start from 0 in order . For example:
<code class="php">$array = [1, 2, 3, 4, 5];</code>
Note:
The above is the detailed content of What is the commonly used function to define arrays in php?. For more information, please follow other related articles on the PHP Chinese website!