Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

PHP Tutorial
PHP Introduction
PHP Install
PHP Syntx
PHP Variables
PHP Operators
PHP Loops & Arrays
PHP Functions
PHP Forms
PHP Includes & Require
PHP File Handling
PHP Cookies
PHP Session
PHP E-mail
PHP SQL Database
PHP Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


Loops and Arrays in PHP

Previous Next



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";
++$x;
}

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]<br>";
++$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.




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords:php tutorial, php scripts, php nuke, php download, php editor, php mysql, php forum, php add link, learn php, php code


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.