To initialize an array in PHP, you can use array() or square brackets.
In this example,
numbers
using the array()
function with integer values.print_r()
.<?php
$numbers = array(1, 2, 3, 4, 5);
print_r($numbers);
?>
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
In this example,
fruits
using square brackets with string values.print_r()
.<?php
$fruits = ['Apple', 'Banana', 'Orange'];
print_r($fruits);
?>
Array ( [0] => Apple [1] => Banana [2] => Orange )
In this tutorial, we learned How to Initialize an Array in PHP language with well detailed examples.