Webucator's Free PHP Tutorial

# Lesson: Arithmetic Operators and Loops

Welcome to our free PHP tutorial. This tutorial is based on Webucator's Introduction to PHP Training course.

Arithmetic Operators are used to perform math operations in PHP.

Loops are used to run the same code a number of times until some condition is met.

Lesson Goals

• To use PHP operators.
• To use the modulus operator.
• To work with loops in PHP.

## Arithmetic Operators

Arithmetic operators in PHP are similar to those found in many modern C-like programming languages.

Arithmetic Operators
Operator Name Example
`+` Addition `\$a + \$b`
`-` Subtraction `\$a - \$b`
`*` Multiplication `\$a * \$b`
`/` Division `\$a / \$b`
`%` Modulus (Remainder) `\$a % \$b`

Assignment Operators
Operator Name Example
`=` Assignment `\$a = 1;`
`\$c = 'Hello' . ' world!';`
`+=`
`-=`
`*=`
`/=`
`%=`
`.=`
Combination Assignment (`\$a+=3` is the same as `\$a=\$a+3`) `\$a += 3;`
`\$a -= 4;`
`\$a *= 5;`
`\$a /= 2;`
`\$a %= 2;`
`++` Increment By One (`\$a++` is the same as `\$a=\$a+1` or `\$a+=1`) `\$a++;`
`++\$a;`
`--` Decrement By One (`\$a--` is the same as `\$a=\$a-1` or `\$a-=1`) `\$a--;`
`--\$a;`

## The Modulus Operator

The modulus operator (`%`) is used to find the remainder after division:

```5 % 2 // returns 1
11 % 3 // returns 2
22 % 4 // returns 2
22 % 3 // returns 1
10934 % 324 // returns 242```

The modulus operator is useful for determining whether a number is even or odd:

```1 % 2 // returns 1: odd
2 % 2 // returns 0: even
3 % 2 // returns 1: odd
4 % 2 // returns 0: even
5 % 2 // returns 1: odd
6 % 2 // returns 0: even```

## Loops

As the name implies, loops are used to loop (or iterate) over code blocks. The following section shows the syntax for different types of loops. Each loop will return "12345".

The loop demos can all be found in OperatorsAndLoops/Demos/loops.html.

There are several types of loops in PHP:

• `while`
• `do...while`
• `for`
• `foreach` (covered when we cover arrays)

### while

`while` loops are used to execute a block of code repeatedly while one or more conditions is `true`.

```\$a=1;
while (\$a < 6) {
echo \$a;
\$a++;
}```

### do...while

`do...while` loops are used to execute a block of code repeatedly until one or more conditions is found to be `false`. The difference between `while` loops and `do...while` loops is that the condition is checked after the code block is executed. This means that, in a `do...while` loop, the code block will always be executed at least once.

```\$a=1;
do {
echo \$a;
\$a++;
}
while (\$a < 6);```

### for

A `for` loop takes three expressions separated by semi-colons and grouped in parentheses before the block to be iterated through.

1. The first expression is executed once before the loop starts. It is usually used to initialize the conditions.
2. The second expression is evaluated before each iteration through the loop. If it evaluates to `false`, the loop ends.
3. The third expression is executed at the end of each iteration through the loop. It is usually used to make changes that can affect the second expression.
```for (\$a=1; \$a < 6; \$a++) {
echo \$a;
}```

### break and continue

To break out of a loop, insert a `break` statement.

## Syntax

```for (\$a=1; \$a < 6; \$a++) {
echo \$a;
if (\$a > 3) 	{
break;
}
}```

This will output: `123`. Then it will break out of the loop.

To jump to the next iteration of a loop without executing the remaining statements in the block, insert a `continue` statement.

```for (\$a=1; \$a < 6; \$a++) {
if (\$a === 3) 	{
continue;
}
echo \$a;
}```

This will output `1245`. It skips `3`.

## Working with Loops

Duration: 10 to 15 minutes.
1. Open OperatorsAndLoops/Exercises/loops.php in your editor.
2. Under the while header, use a `while` loop to output all the even numbers that are less than or equal to 100.
3. Under the for header, use a `for` loop to output all the odd numbers that are less than or equal to 100.

## Challenge

In both loops, skip all numbers that are divisible by 3. Be careful not to get caught in an infinite loop.

## Solution:

OperatorsAndLoops/Solutions/loops.php
```<!DOCTYPE HTML>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Loops</title>
<body>
<main>
<h2>while</h2>
<ul>
<?php
\$a=2;
while (\$a <= 100) {
echo "<li>\$a</li>";
\$a+=2;
}
?>
</ul>

<h2>for</h2>
<ul>
<?php
for (\$a=1; \$a <= 100; \$a+=2) {
echo "<li>\$a</li>";
}
?>
</ul>
</main>
</body>
</html>```

## Challenge Solution:

OperatorsAndLoops/Solutions/loops-challenge.php
```---- C O D E   O M I T T E D ----
<h2>while</h2>
<ul>
<?php
\$a=2;
while (\$a <= 100) {
if (\$a % 3 === 0) {
\$a+=2;
continue;
}
echo "<li>\$a</li>";
\$a+=2;
}
?>
</ul>

<h2>for</h2>
<ul>
<?php
for (\$a=1; \$a <= 100; \$a+=2) {
if (\$a % 3 === 0) {
continue;
}
echo "<li>\$a</li>";
}
?>
</ul>
---- C O D E   O M I T T E D ----```