Logo Tutorials, Ebooks, Source Codes, Question Bank
Submit your Email ID & receive intimation about New/Updated Contents :
Advertise with us             CONTACT US
Android MobilesHTC MobilesMicromax MobilesMobile ArticlesMobile TermsNokia MobilesSamsung MobilesSony MobilesTablets
108 Names of GodBhajans CHALISA CollectionDevi Maa Ki AartiyanGanesh Ji Ki AartiyanHanuman Ji Ki AartiyanRadha Krishan Ji Ki AartiyanRam Ji Ki AartiyanSai Baba Ji Ki AartiyanShiv Ji Ki AartiyanTemples in IndiaVishnu Ji Ki Aartiyan
Wallpapers Pack
Bollywood FlashsFestival FlashsFlash GamesFlash MessagesFunny FlashsPatriotic FlashsPolitical FlashsSpiritual Flashs
FunStuffJokesRajnikanth JokesWhats App JokesWhats App Messages
About IndiaGeneral Knowledge Hindi RhymesMythological StoriesNursery RhymesPatriotic SongsRiddlesShort StoriesTongue Twisters
Funny SlideshowsInteresting SlideshowsMessage SlideshowsNice SlideshowsPatriotic SlideshowsSpiritual Slideshows
BirthDay SMSChristmas SMSClassic SMSFathers Day SMSFestivals SMSFriendship SMSFunny SMSHoli SMSHusbandWife SMSIndependenceDay SMSLohri SMSLove SMSMorning SMSMothers Day SMSNew Year SMSNight SMSPoetry SMSPunjabi SMSRakhi SMSSanta Banta SmSTeachers Day SMSTimePass SMSValentine Day SMS
3D WallpapersAbstract_WallpapersAnimated_WallpapersApple_Mac_WallpapersBaby_WallpapersBackGroundsBeach WallpapersBirds WallpapersCars WallpapersCartoon_WallpapersChristmas WallpapersColorFul_WallpapersColoringSheetsCool_WallpapersFish_WallpapersFlagsFlash TattoosFlowersFunny WallpapersGalleryGodsGreen_ScenesHD_High_ResolutioniPhone WallPapersLinux_WallpapersMobile_WallpapersMortality WallpapersMountain_WallpapersNatureOptical_IllusionsRosesSea_WallpapersShips WallpapersSports_GamesUltraBeautiful WallpaperValentine WallpapersVehiclesWideScreen WallpapersWildLife WallpapersWindows VistaWindowsXPWindows_7_Wallpapers
AJAX ASP.Net 2.0/3.5 C# / C-Sharp C/C++ Cryptography Data Compression Data Structures FLASH Foxpro/VFP HTML JAVA Java Scripts JSP LINUX Microsoft Office Microsoft Windows MS XNA PHP/MySQL Silverlight Software Engineering SQL SQL Server 2005 UNIX VB.Net 2005 VBScript XML
Home Search

PHP - Working with Loops and Arrays

The WHILE Loop

The WHILE loop seems very useful when we want to execute a piece of code for several times. The loop keep executing the code until certain condition is reached. This is one of the most useful command in PHP.
If you want to execute a piece of code for several times without retyping, You can make use of a while loop. For example if you have to print "Hello World" 5 times, you can use the following code
$times = 5;
$x = 0;
while ($x < $times)
echo "Hello World";
In the first two lines we are just setting variables. $time holds number of times the code to be executed. $x is used to count the number of times the code has executed. Then there comes the while loop statement, This tells the processor to execute the code while $x is less than or equals $time. And this statement is followed by the code enclosed within { }.

Arrays in PHP
An array can hold more than one value, These are special variables used in almost many programming languages. Values in array are stored in its own numbered 'space'. Arrays are very useful specially when using WHILE loop.
Initialising an array is slightly different from initialising a normal variable. Setting up an array is slightly different to setting up a normal variable. In this example let us set up an array with 5 names in it:
$names[0] = 'John';
$names[1] = 'Paul';
$names[2] = 'Steven';
$names[3] = 'George';
$names[4] = 'David';
Here the array elements are numbered from 0. To insert a value to the array you must specify the location in the array by putting a number in the brackets [ ].

Reading From An Array
Reading data from the array is very simple, it is same as we put in data. All that we have to do is refer to array and the number of the data item which we want to read out from the array. So if i want to print the third name of the array, then can use the following code.
echo "The third name is $names[2]";
Which would output:
The third name is Steven

Using Arrays And Loops
One of the best use of loop is to display out all the elements in an array. Loop is used to fetch the elements in an array if the array has huge number of elements. For example consider the following array.
Name 1 is John
Name 2 is Paul
Name 3 is Steven
Name 4 is George
Name 5 is David
use the following code:
$number = 5;
$x = 0;
while ($x < $number)
$namenumber = $x + 1;
echo "Name $namenumber is $names[$x]
In the code we have assigned $namenumber as "1" and $x have assigned "0", because the counting of name should start from "1" and the first name stored in the array is in position "0". The echo statement is executed till $x becomes greater than $number, And thus all the names in array are read out.

allfree-stuff.com - Free Education for ALL - for any queries contact @ webmaster@allfree-stuff.com