Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
An operator is anything that takes one or more values and returns another value. We'll cover a number of different ways to add, subtract, multiply and divide values in PHP.

0:00
So let's talk about operators.

0:02
An operator is anything that takes one or more values and returns another value.

0:08
When we assign a value using an equal sign that was an assignment operator.

0:13
When we added numbers using a plus symbol, that's an arithmetic operator.

0:18
When we subtracted numbers using the minus symbol that's also an arithmetic operator.

0:24
Besides the addition and

0:26
subtraction operators, there are a few more operators we're going to use.

0:30
So let's define a few variables and get started using some arithmetic operators.

0:36
I'll start by defining a few more integers.

0:39
I'll add A equals five,

0:42
and then I'll add another one B equals ten.

0:50
We've got two different variables that contain different values.

0:53
We already use the plus sign for addition and minus for

0:56
subtraction, so let's try a few more operators.

1:00
Let's display the product using the multiplication operator,

1:03
which is the asterisk.

1:04
Var_dump A times B.

1:13
Let's add one more here.

1:14
I'll do the quotient.

1:15
The quotient uses the division operator, which is the forward slash.

1:20
We'll use, var dump A divided by B.

1:26
Now let's run our script.

1:27
We see that a multiplied by B gives us the integer 50.

1:33
5 multiplied by 10 equals 50.

1:36
We also see that A divided by B gives us a float, 0.5.

1:43
5 divided by 10 equals 0.5.

1:47
So we can see that even though the variables in these equations are integers,

1:52
the results will be returned in whichever variable type is needed.

1:55
The next operators that I wanna talk about are the incrementing and

1:59
decrementing operators.

2:02
These are often used within a loop.

2:03
I'll be covering loops in a later course, but for

2:06
now, let's just say we want to add one to our variable a.

2:11
We could do a equals a plus 1,

2:17
and then do a vardump.

2:23
Now let's run our script.

2:24
And we see that a equals the integer 6.

2:29
5 plus 1 equals 6.

2:32
We can also do this with PHP's incrementing operator.

2:37
I'll duplicate this var dump line, and then I'll do A plus plus.

2:47
Now let's run our script again.

2:50
A now equals 7.

2:51
Using plus plus is the same as using plus 1.

2:56
Six plus one equals seven.

2:59
The opposite of that is a decrementing operator.

3:02
If I duplicate the var dump once more I can add a minus minus,

3:12
now when I run the script, A equals 6 again.

3:17
7 minus 1 equals 6.

3:20
We can use these operators before or after the variable.

3:24
The end result is the same, but they work slightly different.

3:28
If we put the assignment within a var_dump, you can see what's happening.

3:45
Now when we run our script the second from the last equals 6,

3:50
but the next dump evaluates as 5.

3:54
If I add the operator before the variable it will first subtract and

3:58
then return the value.

4:01
Let's do minus minus A and save the file.

4:08
So the last two dumps both equal four.

4:12
There's one last set of operators that we should talk about before moving on.

4:16
If we want to add or subtract more than one at a time, we do have another option.

4:22
There are combined operators that combine assignment with arithmetic operators.

4:27
For example, if we want to increase the value of A by five,

4:32
we could do a equals a plus 5.

4:38
Then we var_dump our a.

4:44
Now we can run the script, and we can see that a now equals 9.

4:49
4 plus 5 equals 9.

4:54
We can also combine those operators for a shorter version.

4:57
Let's do.

5:01
A plus equals five.

5:06
Now when we run our script, we can see that a equals fourteen.

5:11
9 plus 5 equals 14.

5:15
These combined operators work with any of their arithmetic operators,

5:19
addition subtraction, multiplication and division.

5:24
That was a lot of numbers and a lot of different operators.

5:27
Feel free to rewatch the video to review anything you may have missed.

5:32
Most importantly, make sure you try these operations out for yourself.

5:37
You'll understand and

5:38
remember the coding concepts much more, if you've written the code yourself.
You need to sign up for Treehouse in order to download course files.
Sign up