← Back to PHP Course | Chapter 7: Arrays | Lesson 1 of 13

PHP Arrays Introduction

An array is like a row of lockers under one name, where each locker holds a value. It lets you keep a whole list, like all your friends' names, in one variable.
Syntax
php
$array = [value1, value2, value3];
$array = array(value1, value2);   // older syntax

print_r($array);
count($array);

What is an Array

An array in PHP is a single ordered collection that can hold many values under one variable name, letting you group related data — like a list of usernames — instead of creating a separate variable for each item.

Example: What is an Array

php
<?php
// Declare `$usernames` as an array: `["alice", "bob", "carol"]`
$usernames = ["alice", "bob", "carol"];
// Print a human-readable dump of `$usernames`
print_r($usernames);
?>

Printing Arrays

PHP arrays are unusually flexible: the same array() or [] structure can behave as a numerically indexed list, an associative key-value map, or a mix of both, unlike languages that force a strict choice between the two.

Example: Printing Arrays

php
<?php
// Declare `$mixed` as an array: `["name" => "Alice", 0 => "first", 1 => "second"]`
$mixed = ["name" => "Alice", 0 => "first", 1 => "second"];
// Print a human-readable dump of `$mixed`
print_r($mixed);
?>

Checking Array Size

You create an array with the short [] syntax (or the older array() function) and can nest arrays inside arrays to represent more complex structures, such as a list of orders where each order is itself an array of details.

Example: Checking Array Size

php
<?php
$orders = [
    ["id" => 1, "item" => "Book"],
    ["id" => 2, "item" => "Pen"],
];
// Print a human-readable dump of `$orders`
print_r($orders);
?>

Checking Array Types

count() tells you how many elements an array holds, which is essential before looping over it or validating that a form submitted the expected number of items.

Example: Checking Array Types

php
<?php
// Declare `$items` as an array: `["a", "b", "c"]`
$items = ["a", "b", "c"];
// Print `count($items)` to the output
echo count($items);
?>

Iterating with Foreach

Arrays are passed by value by default in PHP, meaning a function that receives an array and modifies it is working on a copy unless you explicitly pass it by reference with an & — a detail that trips up many beginners.

Example: Iterating with Foreach

php
<?php
function addItem($arr) {
    $arr[] = "new";
}
$list = ["a", "b"];
addItem($list);
print_r($list); // unchanged -- array was passed by value
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Printing an array with echo $arr, which outputs the word Array and a notice; use print_r or var_dump.
  2. Reading a key that does not exist, which raises a warning about an undefined array key.
  3. Assuming count($arr) gives the last index, when the last index is count($arr) - 1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.