PHP is a commonly used server-side programming language and is widely used in Web development. Array is one of the most commonly used data types in PHP, used to store a series of related data. This article will briefly describe how to create an array in PHP.
1. Use the array() function to create an array
In PHP, use the array() function to quickly create an array. The syntax of this function is as follows:
array(value1, value2, value3, ..., valuen)
Among them, value1~valuen represents the elements of the array, which can be numbers, strings, Boolean values, or other basic data types. Here is an example:
$fruits = array("apple", "banana", "orange", "grape");
The above code creates an array $fruits containing 4 elements, namely "apple", "banana", "orange" and "grape".
In addition to using literals to define array elements, you can also use variables or expressions to define them, as follows:
$name = "John"; $age = 25; $city = "New York"; $info = array($name, $age, $city); $sum = array(1 + 2, 3 * 4, 5 / 2);
2. Use the [] operator to create an array
PHP5.4 and above supports the use of the [] operator to create arrays. This operator is used to specify the subscript of the array element when assigning values. The syntax is as follows:
$array = [key1 => value1, key2 => value2, key3 => value3, ..., keyn => valuen];
Among them, key1~keyn are the subscripts of the array elements. The index can be a number or a string, and value1~valuen represents the value of the array element. The following is an example:
$person = ["name" => "Tom", "age" => 30, "gender" => "male"];
The above code creates an associative array $person containing 3 elements, namely "name", "age" and "gender".
You can also omit the subscript and let PHP automatically fill in the numeric subscript, as follows:
$array = [value1, value2, ..., valuen];
Note that at this time, the subscript automatically increases from 0 to n-1.
$colors = ["red", "green", "blue"];
The above code creates an index array $colors containing 3 elements, namely "red", "green" and "blue", whose subscripts are 0, 1 and 2 respectively.
3. Use the range() function to create an array
The range() function can create an array containing all integers within the specified range. Its syntax is as follows:
range(start, end, step)
Among them, start is the starting value of the array, end is the ending value of the array, and step is the step size between array elements. The following is an example:
$numbers = range(1, 10, 2);
The above code creates an array $numbers containing 5 elements, which are 1, 3, 5, 7 and 9.
4. Use the list() function to create an array
The list() function can create an array and assign variables to the elements of the array. The syntax is as follows:
list($var1, $var2, $var3, ..., $varn) = array(value1, value2, value3, ..., valuen);
Among them, $var1~$varn are variable names, used to store the corresponding array element values, and value1~valuen are the array element values. The following is an example:
list($name, $age, $city) = array("John", 25, "New York");
The above code creates an array containing 3 elements, namely "John", 25 and "New York", and assigns them to $name, $age and $ in turn. city variable.
To sum up, the methods of creating arrays in PHP include using the array() function, [] operator, range() function and list() function. Developers can choose a method that suits them based on actual needs.
The above is the detailed content of PHP briefly describes how to create an array. For more information, please follow other related articles on the PHP Chinese website!