Home >>Advance PHP Tutorial >PHP Array

PHP Array

PHP array

All the variables you have used have held only a single value. Array variables are "special" because they can hold more than one value in one single variable. This makes them particularly useful for storing related values.

There are three types of array.

  • Indexed Array : An array with a numeric index.
  • Associative Array : An array where each key is associated with a value.
  • Nesting Array(Multi-D array) : An array containing one or more array.

How to define array

<?php	
$arr = array(10,11,12,13,14,15);		
echo $arr[0]; 		
?>
Output 10

In the above example This method is to create a numeric array. Here index are automatically assigned to array element. All values are store in the form of array corresponding to their index value. i.e $arr[0]=10,$arr[1]=11 and so on. we want to print first value of array, for this we call array name with their index ($arr[0]): So output is 10.

Eg ii (Define array)

 <?php	
$arr[ ]=10;
$arr[ ]=20;
$arr[ ]=30;
$arr[ ]=40;
$arr[ ]=50;
echo $arr[0]; 
?>
Output 10

In the above example This is second method to create a numeric array. Here also index assigned automatically. The output is same as previous one.

Eg iii (Define array)

<?php
	
$arr[0]=10;

$arr[1]=20;
		
$arr[2]=30;
                
$arr[3]=40;
                
$arr[4]=50;
	      
echo $arr[0]; 
		
?>

Output 10

In the above example This method is also to create numeric array. But indexed assigned manually in this program. The output is same as previous one

Eg iv (Define array in PHP 7)

<?php
$arr=[10,20,30,40,50];
echo $arr[0]; 		
?>
Output 10

Popular Tutorials