CopyPastehas never been so tasty!

Create Initialize Arrays in PHP

by anonymous

  • 0
  • 0
  • 0

Purpose of Array


An array stores many values in one identifier. An identifier is a container that holds a number or text. The issue is, identifier will hold only one value. An array is a particular type of identifier, which can store many values in one single identifier.


In an array we store whatever we want numbers, strings on its index’s etc.





1.    <?php

2.    $names_of_students = array("Tahreem", "Anwar", "Rizwan","Fatima");


3.    $names_of_students[1] = "Saba";

4.    $names_of_students [4] = "Misbah";


5.    echo "First Index of array= &nbsp;".$names_of_students[0]."<br/>";

6.    echo "Second Index of array=&nbsp;".$names_of_students[1]."<br/>";

7.    echo "Third Index of array=&nbsp;".$names_of_students[2]."<br/>";

8.    echo "Fourth Index of array=&nbsp;".$names_of_students[3]."<br/>";

9.    echo "Fifth Index of array=&nbsp;".$names_of_students[4]."<br/>";

10.  ?>





First Index of array= Tahreem

Second Index of array= Saba

Third Index of array= Rizwan

Fourth Index of array= Fatima

Fifth Index of array= Misbah






On line2 we have created $names_of_students and initialize it. The values are assigned by index values that starts from ‘0’.On line 3 we overwrite index [1] value by assigning new value “Saba”. On line 4 we have added new index in array index [4] and assign a value “Misbah” to index [4].





1.    <?php

2.    $Roll_of_students = array();

3.    $Roll_of_students[0]=1;

4.    $Roll_of_students[1]=2;

5.    echo "First index roll no=".$Roll_of_students[0];

6.    echo "<br/>";

7.    echo "Second index roll no=".$Roll_of_students[1];

8.    ?>





First index roll no=1

Second index roll no=2






On line 2 we have created a array type variable by assigning it array().Then online 3 we have initialized it by  values index[0]=1,index[1]=2 and then print it.


Add A Comment: