PHP Array
The array is a collection of data that store the fixed number of values of the same type.
Syntax:-
$arrayReferenceVariable = array_fill(0, $arraySize, initialValue);
Example:
if we want to store marks of 100 students, then we can create an array for it.
<?php // Create an array of size 5 filled with 0 $intArray = array_fill(0, 5, 0); // Accessing elements echo $intArray[0]; // prints 0 // Updating an element $intArray[2] = 10; ?>
Size and type of arrays cannot change after it's declared.
Two types of Arrays:
- One-dimensional arrays
- Multidimensional(2D) arrays

| Array Type | Description |
|---|---|
| Single Dimensional array | In this we create and initialize all the values in the single array. |
| Multi-Dimensional Array | PHP supports the multi-dimensional array. The multi-dimensional array also called as the Two-Dimensional array. |
Advantages of an array:
- It is better to store the data of the same data type with the same size.
- Coding is less, one variable can store a number of values.
- It allocates memory in different memory locations for its elements. There is no memory flow or shortage of memory in arrays.
- Iterating is faster in arrays compared to other methods like a linked list.
- By using array data retrieving is easy.
- Easily short the data using the swapping method.
- By array index, we can randomly access any element from the array.
- It supports multidimensional array.
Array Declaration in PHP
To declare an array in PHP programming language.
Syntax:-
<?php // Define the array size $arraySize = 5; // Create an indexed array with default values (e.g., 0 or null) $arrayReferenceVariable = array_fill(0, $arraySize, 0); // Fill with 0 ?>
Array Initialization in PHP
An array is initialized by using the index of each element.
Syntax:-
$score = [100, 221, 59, 54];

Example:-
<?php
$score = [100, 221, 59, 54];
for ($i = 0; $i < count($score); $i++) {
echo "Element[" . $i . "] = " . $score[$i] . "\n";
}
?>
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com