PHP Introduction | Control Structures | while and do while Statements
Next, let us look at statements that perform “repetition.” Repetition means running the same kind of processing multiple times. There are several kinds of loop statements, but the easiest to understand is probably the statement that uses while.
1. Check the condition first.
while (condition) {
...... repeated processing ......
}
You can also use a colon (:) and endwhile instead of braces.
while (condition):
...... repeated processing ......
endwhile;
2. Check the condition at the end.
do {
...... repeated processing ......
} while (condition);
These statements provide two ways to repeat processing: check the condition before running the repeated block, or run the repeated block first and then check the condition. As with if, the condition is an expression or value represented as a logical value, either true or false.
In general, the “check first” form is used more often. The form that checks afterward runs the repeated block even when the condition is false from the beginning, so it is used only in somewhat special cases.
Let us look at a simple example.
<?php
$num = 1;
$total = 0;
while($num <= 100){
$total += $num;
$num++;
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<div>
<?php echo "Total: " . $total; ?>
</div>
</body>
</html>
This example displays the sum of all integers from 1 to 100. Here, the condition is written as while ($num <= 100), so the loop continues while the value of $num is 100 or less.
Assignment Operators
The processing being performed is the calculation $total += $num;. This means “add $num to $total.” This is an “assignment operator,” which assigns a value to the variable on the left side, like the equals sign (=). The main symbols are as follows.
| Expression | Description |
|---|---|
| A += B | Adds B to A and assigns the result to A. |
| A -= B | Subtracts B from A and assigns the result to A. |
| A*= B | Multiplies A by B and assigns the result to A. |
| A /= B | Divides A by B and assigns the result to A. |
| A %= B | Divides A by B and assigns the remainder to A. |
These assignment operators are easier-to-write forms of assignment expressions that use the ordinary equals sign (=). For example, $total += $num; has the same meaning as $total = $total + $num;. Comparing the two, $total += $num; is cleaner and easier to understand.
Increment Operators
The example also runs $num++;. The symbol ++ increases a value by 1. This is called an “increment operator.”
Similarly, there is also a “decrement operator,” written as --. It decreases the value of a variable by 1.
The ++ and -- operators can also be included inside expressions. In the current example, you could write it as follows.
$total += $num++;
This lets the two lines be written as a single statement.
These operators can be written either before or after the variable. For example, you can write ++$num or $num++. Be careful, because these two forms behave slightly differently.
When the operator is placed before the variable, 1 is added or subtracted before the variable’s value is used in the expression. When the operator is placed after the variable, the current value is used in the expression first, and then 1 is added or subtracted. In other words, the timing differs between using the variable and increasing or decreasing it.
You will naturally remember this as you use it many times. For now, keep in mind that these symbols exist.