PHP 5 Array
Arrays
Arrays are complex variables that store a group of element under a single variable name, Indexed by the number or strings. In case of number it always starts from zero.
Example: An array variable $number store many number but in case of general variable it’s store only one value.
In case of general variable:
$number = 10;
In case of array:
$number = array (10, 20, 30);
You can easily access array element using index number that always start from zero.
First number :-$number[0]
Second number :-$number[1]
Third number :-$number[2]
Sum of an array using for loop
<?php echo "Sum of an array using for loop"."
"; $numbers = array( 8, 5, 7, 12, 50); $a=count($numbers); $sum=0; for( $i=0; $i < $a; $i++ ) { $sum=$sum+$numbers[$i]; } echo $sum; ?>
- Example
- Run
Feature of arrays
- Arrays can be of any length - An array can store one value, or millions of values, all referenced via a single variable name
- It's easy to manipulate all values in an array.
- An array is useful for storing a group of related values
- Easily handle any of the element in array
An array starts from zero, that's shown in above example.
Types of array
There are three type of array:-
- Numeric array
- Associative array
- Multidimensional array
All three arrays has been explained in next sections.