Declaring a new array in php
In PHP, you can declare a new array using the following syntax:
$array_name = array();
Or, you can use the shorthand syntax:
$array_name = [];
Both of these methods will create a new empty array.
For example:
$fruits = array();
$fruits = [];
Both of these will create a new array called $fruits
that is empty.
You can also declare an array with initial values using the following syntax:
$array_name = array('value1', 'value2',...);
Or, you can use the shorthand syntax:
$array_name = ['value1', 'value2',...];
For example:
$colors = array('red', 'green', 'blue');
$colors = ['red', 'green', 'blue'];
Both of these will create a new array called $colors
with the values 'red'
, 'green'
, and 'blue'
.
You can also use the array()
function to create an array with initial values:
$numbers = array(1, 2, 3, 4, 5);
Note that in PHP, arrays are a type of data structure that can store multiple values, and are often used to store collections of data.