We have variables in all programming languages to save something in them like var name = Ateeq’ which means name holds ateeq name. But what if we want to save 100 names of people? and show them in a list somewhere? will we create 100 Variables for them ?
Yes we can create 100 Different variables like name1, name2, name3….. Or there is a simple way using array actually array holds many variables in it, like if we add new array like this
$my_array = (ateeq1, ateeq2, ateeq3, ateeq4…..);
Now $my_array holds 100 names in it this thing saves a lot of time. To access them we can do this suppose you want to access a name which is saved in 50 Number
you will do so echo $my_array[’50’];
There are loops which help you a lot to grab everything from array in nice manner an example is below declaring an array and then showing their data on table format using foreach loop. Remember Array index starts from 0 means if your array holds 5 names that means arra(0), array(1) 2 3 4 so 0 1 2 3 4 are 5 indexes of names.
[php]<?php /*PHP Tag starts here.*/
$my_array = (Ateeq, Abbas, Umer, Abubakar, Ali, Ramiz, Usman, Nisar, Sadiq, Irfan); //We have declared an array which holds 10 names.
$counter = 0; //we are declaring a counter to count what we have done so far.
$table = ”; //declaring table variable which will concatinate all table elements.
foreach($my_array as $val) {
$table .= ‘<tr>td>’;
$table .= $counter+1; // here i will print number. As you know my counter starts from 0 i added 1 to start visitors visibility from 1
$table .= ‘</td><td>’;
$table .= $val;
$table .= ‘</td></tr>’;
}
?>[/php]
There are other types of array as well I will list them later, Please post your questions if you need any help.