In PHP, arrays are used to store multiple values. There are two common ways to declare an array: the square bracket method and the array() function method.
The square bracket method is the simplest way to declare an array. You simply enclose the array elements in square brackets and separate them with commas. For example:
$array = [1, 2, 3, 4, 5];
In addition to square brackets, you can also use the built-in array() function to declare arrays. Pass the elements as parameters to the function. For example:
$array = array(1, 2, 3, 4, 5);
PHP allows you to create associative arrays, where data is stored in key-value pairs. You can use the following syntax to set key-value pairs in an array:
$array['name'] = 'John Doe';
For example:
$array = [
'name' => 'John Doe',
'age' => 30
];
You can access array elements using square brackets or the array_get() function. For example:
$name = $array['name'];
Or:
$age = array_get($array, 'age');
With these two methods, you can easily declare and manipulate arrays in PHP. Mastering these basic array operations will help you become more efficient in PHP programming.