Looping statements in PHP are used to carry out the same block of code a specified number of times.
Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.
In PHP There are four looping statements:
(1) do…while – loops through a block of code once, and then repeats the loop as long as a special condition is true
(2) for – loops through a block of code a specified number of times
(3) while – loops through a block of code if and as long as a specified condition is true
(4) for each – loops through a block of code for each element in an array
(1) The do…while Statement
The do…while statement will execute a block of code at least once – it then will repeat the loop as long as a condition is true.
Syntax
do
{
Code to be execute;
}
while (condition);
Example
The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 5:
<html>
<body>
<?php
$j=0;
do
{
$j++;
echo “The figure is ” . $j . “<br />”;
}
while ($j<5);
?>
</body></html>
(2)The for Statement
The for statement is used when you know how many times you want to execute a statement or a list of statements.
Syntax
for (initialization; condition; increment)
{
code to be executed;
}
Message: The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.
Example
The following example prints the text “Welcome” five times:
<html>
<body>
<?php
for ($j=1; $j<=5; $j++)
{
echo “Welcome<br />”;
}
?>
</body>
</html>
(3)The while Statement
The while statement will execute a block of code if and as long as a condition is true.
Syntax
while (condition)
Code to be executed;
Example
The following example demonstrates a loop that will continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
<?php
$j=1;
while($J<=5)
{
echo “The figure is ” . $j . “<br />”;
$j++;
}
?>
</body>
</html>
(4)The for each Statement
The for each statement is used to loop through arrays.
For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) – so on the next loop, you’ll be looking at the next element.
Syntax
For each (array as value)
{
Code to be executed;
}
Example
The following example demonstrates a loop that will print the values of the given array:
<html>
<body>
<?php
$arr=array(“one”, “two”, “three”);
foreach ($arr as $value)
{
echo “Value: ” . $value . “<br />”;
}
?>
</body>
</html>
An array can store one or more values in a single variable name.
What is an array?
When working with PHP, earlier or later, you might want to create many similar variables.
In place of having many similar variables, you can store the data as elements in an array.
Each element in the array has its own ID so that it can be easily accessed.
There are three different types of arrays:
1 Numeric array – An array with a numeric ID key
2 Multidimensional arrays – An array containing one or more arrays
3 Associative arrays – An array where each ID key is associated with a value
(1) Numeric Arrays
A numeric array stores each part with a numeric ID key.
There are different ways to create a numeric array.
Example
In this example the ID key is automatically assigned:
$names = array(“Akki”,”Jimmi”,”Joy”);
In this example we assign the ID key manually:
$names[0] = “Akki”;
$names[1] = “Jimmi”;
$names[2] = “Joy”;
The ID keys can be used in a script:
<?php
$names[0] = “Akki”;
$names[1] = “Jimmi”;
$names[2] = “Joy”;
echo $names[1] . ” and ” . $names[2] .
” are “. $names[0] . “’s partner”;
?>
The code above will output:
Jimmi and Joy are Akki’s partner
(2)Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
Example
In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
(“Griffin”=>array
( “Akki”,”Pats”, “Maks”),
“Jimmi”=>array
(“Glenn” ),
“Brown”=>array
(“Cleveland”, “Loretta”,”Junior”));
The array above would look like this if written to the output:
Array
([Griffin] => Array
([0] => Akki
[1] => Pats
[2] => Maks )
[Jimmi] => Array
( [0] => Glenn )
[Brown] => Array
( [0] => Cleveland
[1] => Loretta
[2] => Junior))
3 Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
Example
In this example we use an array to assign ages to the different persons:
$ages = array(“Akki”=>21, “Jimmi”=>18, “Joy”=>20);
This example is the same as example 1, but shows a different way of creating the array:
$ages['Akki'] = “21″;
$ages['Jimmi'] = “18″;
$ages['Joy'] = “20″;
The ID keys can be used in a script:
<?php
$ages['Akki'] = “21″;
$ages['Jimmi'] = “18″;
$ages['Joy'] = “20″;
echo “Akki is ” . $ages['Akki'] . ” years old.”;
?>
The code above will output:
Akki is 21 years old.
Description: Learn how to use MySQL syntax with these references.
AND: This will do our command to two or more settings, I think reading the code will explain more than I can try to in normal English…
INSERT: Inserts a new row into an existing table of your database with the values you define for each field. Used in conjunction with mysql_connect() mysql_select_db() mysql_query()
ALTER: ALTER will change the table you specify, see the code example for a more clear idea of how it works…
NOT: This will SELECT all records from the db table WHERE ’something’ is NOT equal to ‘whatever’.
OR: The OR command will SELECT either one or the other… (again it is easier to look at the code to make sense of it.)
ORDER BY: Using ORDER BY will sort the results in a specific order based on the values of the desired column: see the example for more details…
CREATE TABLE: Using CREATE TABLE will do exactly that – create a new sql table in your database. See the example code for the syntax, each column you define also has to have a column data-type, this can be ‘text’, ‘char’, ‘blob’ amongst many, many
SELECT: The SELECT statement will select ’something’ FROM ’sometable’.
UPDATE: Using UPDATE will update a specified record with a new value. Or record in the db table.
DELETE: Using DELETE will remove (delete, even!) the record (row in the table) from the database. Usually used with a WHERE clause to restrict the deletion to a certain record or set of records.
WHERE: Used in conjuction with a SELECT statement, using WHERE will restrict the SELECT to a certain rule…
PHP has been around for a few years now and has developed as a language. Why should you use it over ASP, JSP, or Perl when you are building a web application? With a veritable alphabet of programming languages to choose from when you decide to write a web based application, why should you choose PHP over ASP, JSP, Perl, CGI, or Cold synthesis? While any decision you make also has to be based on the business supplies of the application, as a consultant you often have the choice of what language to use. So barring an IT department that tells you “It’s Microsoft or nothing!” or a gaggle of UNIX developers demanding that all their applications be Java based, here are a few reasons why PHP is the best solution for web applications. It was created for the web – PHP started as a collection of C applications that the author wrote for use on his own web site. It evolved into a server-side scripting language built for building logic into web pages. This firmness in design has resulted in a language that is both flexible and streamlined for use on the web. Its cross-platform – PHP runs on UNIX, Linux, Mac OSX, and Windows. In addition to being cross-platform, it has built in functions for connecting to most popular database, including general ODBC connections. It’s cross-language – What does that mean? It’s a term I coined to show that PHP can use objects in languages other than PHP. It can call Windows COM objects and Java objects, making it easier to separate the business logic from the user interface. The PHP team is even working on letting it call .NET meeting. The syntax is based on C – Being based on C makes it easier for Java, C, even JavaScript programmers to pick up. It also has built in functions for handling regular expressions, XML parsing, and cryptography. You can compile in different libraries that allow you to manipulate graphics. One feature of the language where it breaks from the C syntax is it’s object-oriented nature. It supports the creation of custom classes and allowing other classes to inherit from your custom classes. Future releases should further enhance it’s object-oriented capabilities. PHP is much faster than CGI – It’s not as fast as JSP’s, which are compiled, but it is faster and uses less system resources than CGI programs do. My final reason for choosing PHP over other languages has to do with the popularity of the languages. Thousands of web pages are running PHP and it has a dedicated following. If you choose to learn PHP, you can be assured that you can find enough resources to answer any question you have and in most cases you’ll find that someone has already written code that does exactly what you need and is giving it away for free.
Each of these technologies have their strengths and weakness but overall for beginners getting into the game of dynamic websites There are some reason to choose this language
1. It is so popular that cheap PHP hosting is not hard to find.
2. It is free.
3 It is easy to learn
4. It is flexible and powerful.
5. PHP is probably the most popular web scripting language and environment out today so there is a lot of freely available information out there on it.
In general when a new software version appears everybody hurries to update it particularly if it is for free. PHP 5 has just come into view and it seems that there are people who use the PHP 4 version and others who use the PHP 5 one.
You most likely know that PHP 5 is destined for OOP, but it appears that usual programming can be used too. Furthermore, OOP is used in PHP4 as well, with the difference that in PHP5 things are a little more evaluated. This means that in PHP4 safety modes for classes (public, private) are not accepted. In PHP4 the objects are a kind of structures which accept objects and functions as well, according to OOP. In PHP4 they are useful as well.
If you are used to working with PHP4 you can use PHP5 with no problems because the differences are not important and the changes were made so that programmers would not be confused. An example would be class builders which, in PHP4 were functions within the classes bearing the same name as the class. In PHP5 it is firstly checked if there is a function (method) __construct (). If it does not exist, check if there is a function (method) which has the same name as the class. This means that even if you are not aware of the latest news in the domain of PHP5, your scripts will function without any problem.
If you want to install PHP4 and PHP5 in parallel, you can set USE indicators, which are different for each version and you can find out a lot of other important pieces of information asking us.