**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses 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.

So let's talk about operators.
0:00

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

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

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

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

Besides the addition and
0:24

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

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

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

I'll add A equals five,
0:39

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

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

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

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

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

which is the asterisk.
1:03

Var_dump A times B.
1:04

Let's add one more here.
1:13

I'll do the quotient.
1:14

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

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

Now let's run our script.
1:26

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

5 multiplied by 10 equals 50.
1:33

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

5 divided by 10 equals 0.5.
1:43

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

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

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

decrementing operators.
1:59

These are often used within a loop.
2:02

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

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

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

and then do a var-dump.
2:17

Now let's run our script.
2:23

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

5 plus 1 equals 6.
2:29

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

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

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

A now equals 7.
2:50

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

Six plus one equals seven.
2:56

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

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

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

7 minus 1 equals 6.
3:17

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

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

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

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

but the next dump evaluates as 5.
3:50

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

then return the value.
3:58

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

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

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

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

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

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

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

Then we var_dump our a.
4:38

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

4 plus 5 equals 9.
4:49

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

Let's do.
4:57

A plus equals five.
5:01

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

9 plus 5 equals 14.
5:11

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

addition subtraction,
multiplication and division.
5:19

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

Feel free to re-watch the video to
review anything you may have missed.
5:27

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

You'll understand and
5:37

remember the coding concepts much more,
if you've written the code yourself.
5:38

You need to sign up for Treehouse in order to download course files.

Sign up