PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev
Читать онлайн книгу.($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!
Hello, Anna!
Hello, Robert!
Hello, John!
Hello, James!
Hello, George!
Hello, Maria!
Hello, Peter!
Hello, James!
If and else statement
The if… else statement is used to execute different pieces of code under different conditions.
It has the following construction:
If (conditions) {
Do this.
} elseif (different condition) {
Do that.
} else {
Do something else.
}
Let us create a function that will print a message depending on temperature.
function weather ($F) {
if ($F> 78)
$message=“It is too hot!”;
elseif ($F <65)
$message=“It is too cold!”
else
$message=“Today is nice outside!”;
Return $message.”<br />”;
}
Let us call the function
echo weather (80);
Output: will be “It is too hot!”;
echo weather (60);
Output: will be “It is too cold!”;
echo weather (70);
Output: will be =“Today is nice outside!”;
Switch statement
The switch statement has the following syntax:
switch (variable) {
case label1:
code to be executed if variable =label1;
break;
case label2:
code to be executed if variable =label2;
break;
default:
code