PHP Create Arrays
In this page:
$array = [value1, value2, value3];
$array = array(value1, value2, value3); // older syntax
$array = []; // empty array
Creating an Array with Short Syntax
The modern way to create an array is square brackets: $fruits = ["Apple", "Banana", "Cherry"]. This short syntax is concise, matches the syntax used to access array items, and is the style recommended in current PHP code.
Example: Creating an Array with Short Syntax
<?php
// Declare `$fruits` as an array: `["Apple", "Banana", "Cherry"]`
$fruits = ["Apple", "Banana", "Cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Creating an Array with array()
The older array() function-call syntax, $fruits = array("Apple", "Banana", "Cherry"), produces an identical array to the short [] syntax -- it is fully supported and still common in older codebases, but the short syntax is now generally preferred for new code.
Example: Creating an Array with array()
<?php
// Declare `$fruits`, set to `array("Apple", "Banana", "Cherry")`
$fruits = array("Apple", "Banana", "Cherry");
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Starting Empty and Filling In Values
An array does not need any values at creation time -- $items = [] starts a completely empty array, which you can then fill in afterward, one value at a time, often inside a loop that builds up the array's contents based on some other data.
Example: Starting Empty and Filling In Values
<?php
// Declare `$items` as an empty array
$items = [];
// Append "first" to the end of `$items`
$items[] = "first";
// Append "second" to the end of `$items`
$items[] = "second";
// Print a human-readable dump of `$items`
print_r($items);
?>
Login to try C/C++/Java/PHP code in the editor
Creating an Array with a Specific Size and Default Values
array_fill(startIndex, count, value) creates an array of a given length where every element starts out set to the same default value, which is useful for pre-allocating a fixed-size structure like a grid or a set of default settings before filling in real values.
Example: Creating an Array with a Specific Size and Default Values
<?php
// Declare `$grid`, set to `array_fill(0, 5, 0)`
$grid = array_fill(0, 5, 0);
// Print a human-readable dump of `$grid`
print_r($grid);
?>
Login to try C/C++/Java/PHP code in the editor
Creating an Array from a Range
The range(start, end) function generates an array containing every value from start to end (inclusive), stepping by 1 by default -- a quick way to create a sequential array of numbers or even letters without writing a loop.
Example: Creating an Array from a Range
<?php
// Declare `$numbers`, set to `range(1, 5)`
$numbers = range(1, 5);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
- Using the older array(...) syntax out of habit in new code, when the shorter [...] syntax (available since PHP 5.4) is the modern, preferred style.
- Assuming an array must be created with all its values up front, when starting from an empty array and appending values in a loop is often the more natural approach.
- Mixing indexed and associative-style creation inconsistently within the same array, making its intended structure harder to read at a glance.
- The short syntax, $arr = [1, 2, 3], is the modern, preferred way to create an array literal in PHP.
- The older array(1, 2, 3) function-call syntax still works identically, but is considered legacy style.
- An array can also start empty, $arr = [], and be filled in afterward using assignment or the [] append syntax.
The short [] array syntax has been supported since PHP 5.4; array() has worked since PHP's earliest versions.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: