To declare an array in PHP, you use the array() function or the [] shorthand notation.
In this example,
fruits
using the array()
function and initialize it with some values.print_r()
.<?php
$fruits = array('Apple', 'Banana', 'Orange');
print_r($fruits);
?>
Array ( [0] => Apple [1] => Banana [2] => Orange )
In this example,
person
with key-value pairs using the []
shorthand notation.print_r()
.<?php
$person = ['name' => 'Alice', 'age' => 30, 'city' => 'New York'];
print_r($person);
?>
Array ( [name] => Alice [age] => 30 [city] => New York )
In this tutorial, we learned How to Declare an Array in PHP language with well detailed examples.