PHP Create Arrays
In this page:
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.
Note: Use the short [] syntax by default for all new arrays -- it is shorter to type and is what most modern PHP style guides recommend.
Warning: Forgetting a comma between items in a multi-line array literal is a common typo that produces a confusing parse error.
Example: Creating an Array with Short Syntax
<?php
$fruits = ["Apple", "Banana", "Cherry"];
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.
Note: You will still encounter array() often in existing PHP projects; recognize it as functionally identical to [] rather than something different.
Warning: Mixing array() and [] inconsistently within the same codebase adds no functional difference but can look stylistically inconsistent.
Example: Creating an Array with array()
<?php
$fruits = array("Apple", "Banana", "Cherry");
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.
Note: Starting with an empty array and appending inside a loop is the natural pattern when the array's contents come from processing other data, rather than being known values upfront.
Warning: An empty array is falsy in a boolean context, which is useful for checking "has anything been added yet" with a simple if ($items) check.
Example: Starting Empty and Filling In Values
<?php
$items = [];
$items[] = "first";
$items[] = "second";
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.
Note: Use array_fill when you need a predictable, fixed-size array upfront, like initializing a scoreboard of zeros before any scores come in.
Warning: array_fill's second argument is a count, not an end index -- passing the wrong number produces an array of the wrong length.
Example: Creating an Array with a Specific Size and Default Values
<?php
$grid = array_fill(0, 5, 0);
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.
Note: Use range() for quickly generating sequential test data or numbered options, instead of manually typing out each value.
Warning: range() with a large span (like range(1, 1000000)) creates a correspondingly large array in memory all at once -- be mindful of the size for very large ranges.
Example: Creating an Array from a Range
<?php
$numbers = range(1, 5);
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: