PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev
Чтение книги онлайн.
Читать онлайн книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev страница 7
Break and Continue
Break; statement breaks out of the loop. Loop execution stops.
$names [0] =“Anna”;
$names [1] =“George”;
$names [2] =“James”;
$names [3] =“James”;
$names [4] =“John”;
$names [5] =“Maria”;
$names [6] =“Peter”;
$names [7] =“Robert”;
for ($i=0; $i <sizeof ($names); $i++) {
if ($names [$i] == “John”)
Break;
print ($names [$i].”<br>”;
}
The loop above will print the names:
Anna
George
James
James
and then stops. The names John, Maria, Peter and Robert will not be printed.
Continue; statement makes the loop skip iteration. See the following continue code example:
for ($i=0; $i <sizeof ($names); $i++) {
if ($i == 0)
print ($names [$i].”<br>”);
else
{
if ($names [$i-1] == $names [$i])
continue;
print ($names [$i].”<br>”);
}
}
Output:
Anna
George
James
John
Maria
Peter
Robert
In line if ($names [$i-1] == $names [$i]) we check if a previous name equals a current name. In the first iteration, we do not have a previous name, so we print a name without checking for a duplicate.
Starting from the second iteration, when $i => 1 we check whether a previous name is equal to the current one. If it is, we skip the loop.
As a result, the duplicate name is not printed. The break and continue may be used in all other loops.
The foreach loop
To use the for loop for an array you have to know how many items the array has. Sometimes, it is more convenient to use the foreach loop. The Foreach loop has the following syntax:
foreach (array_expression as $value)
statement
http://php.net/manual/en/control-structures.foreach.php
If you have an array of names ($names) and you want to greet each person, use the foreach loop to avoid having to know how many names are in the array.
foreach ($names as $aname)
echo “Hello,”. $aname.”! <br>”;
Output:
Hello, John!
Hello, George!
Hello, James!
Hello, Anna!
Hello, Robert!
Hello, John!
Hello, James!
Hello, George!
Hello, Maria!
Hello, Peter!
Hello, James!
In the foreach loop we can read and display not only a value of the array element but its key as well.
foreach (array_expression as $key => $value)
statement
foreach ($names as $key=> $aname)
echo $key.” Hello,”. $aname.”! <br>”;
Output:
0 Hello, John!
1 Hello, George!
2 Hello, James!
3 Hello, Anna!
4 Hello, Robert!
5 Hello, John!
6 Hello, James!
7 Hello, George!
8 Hello, Maria!
9 Hello, Peter!
10 Hello, James!
The number is the array element’s index or key.
The while Loop
The ‘while loop’ is used in many programming languages. A code inside a while loop is executed while a condition is true.
When using the while loop, make sure that your condition will become false and the loop stops. Otherwise you will be continuously stuck in the loop.
Let us print our array using the while loop.
while ($i <$asize) {
print (“Hello, ".name [$i].”<br />”;
$i ++;
}
Output:
Hello, John!
Hello, George!
Hello, James!