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
Operators are symbols that help us modify data values. Let's learn about the three types of operators and add some to our project.
Learn more about PHP Operators
*Correction to Operators
Operators can be grouped according to the number of values they take.
Unary operators take only one value, for example ! (the logical not operator) or ++ (the increment operator).
echo $a++;
Binary operators take two values, such as the familiar arithmetical operators + (plus) and  (minus), and the majority of PHP operators fall into this category.
if ($a === $b) {
echo $a;
}
Finally, there is a single ternary operator, ? :, which takes three values; this is usually referred to simply as "the ternary operator" (although it could perhaps more properly be called the conditional operator).
echo $a === $b? $a : 'not equal';
//displays $a if $a is equal to $b
Learn more about PHP Ternary Operators

0:00
[MUSIC]

0:04
So let's talk about operators.

0:06
There are a lot of different types of operators in PHP.

0:10
So we're gonna break this up into a couple different videos here for you.

0:14
But let's start with understanding some basic ones.

0:18
The first is arithmetic and the next is string operators.

0:21
So, an operator is anything that takes one or more values and yields another value.

0:28
So when we're adding numbers, we're using a plus symbol.

0:30
That's an operator.

0:31
When we're using a minus when we're doing subtraction,

0:34
we get a different result by using the subtraction operator, or the dash.

0:39
So let's define some operators and get started using some arithmetic operators.

0:46
And we'll start by defining a few integers.

0:48
So we'll do a.

0:50
Is = 10.

0:51
And then we'll do another one, which will be b.

0:55
We'll set that equal to 10 as well.

0:59
We've got two different variables both with 10 as their value.

1:03
All right, so let's with doing some base arithmetic operators.

1:09
Let's start with sum.

1:10
So, sum.

1:11
And the sum of these two numbers, so we'll do a and then the plus, or

1:16
the addition or sum operator, and then b.

1:20
So we've seen this before, nothing really drastic here, but

1:23
that will give us a sum result of the values of a and b.

1:28
So let's do a few more here.

1:30
So let's say difference, or diff, And

1:33
then we'll do a minus b, that'll give it the difference of the two.

1:40
How about we'll do the product, so product, so

1:44
we'll be doing multiplication here, so we'll do dollar sign a,

1:47
the operator here is the asterisk, so asterisk will do multiplication or

1:51
give you the product of the two, so b close it with a semicolon.

1:57
And then one more here.

1:58
We'll do the quotient.

2:00
So quotient.

2:03
Okay.

2:04
And then to divide, we'll do a.

2:08
And then /$b.

2:12
Okay, so here are four operators that we will use very often with arithmetic.

2:17
So let's echo these out and see what we get.

2:20
We'll actually put them down here in an unordered list.

2:23
So scroll down, and create our unordered list.

2:30
And same thing we'll add a few list items here.

2:37
All right, and let's do some echoing,

2:42
so open up our PHP tags and say echo and then sum.

2:48
Close our PHP tags, and then we'll do a few more here.

2:52
So, we'll copy and paste this in and we will do all of our other operators.

2:59
So, there's differences.

3:01
Here's our product and then our division, or our quotient.

3:06
Save this, and let's do our preview.

3:09
Okay. So 20, 0, 100 and 1.

3:13
So that gives us our addition, which 10 and 10 is 20.

3:16
It gives us our subtraction.

3:18
So 10 minus 10 is 0.

3:20
10 times 10, our multiplication.

3:22
And then 10 divided by 10, our division.

3:25
So there are all of our results.

3:28
The next arithmetic operator that we want to talk about here

3:32
will actually be auto incrementing and auto decrementing.

3:35
This is something that you might want to do as we're looping through things.

3:38
We're gonna get into loops a little bit later, but let's just say we wanna to take

3:42
a value, one of these values, let's say our $product here.

3:47
So $product, and then we want to just add one to it.

3:51
So we could do product equals product plus one.

3:57
So now we'll save this, switch back over and refresh,

4:01
and you'll see our product is 101.

4:05
Let's actually remove all these other ones by just simply commenting them out

4:09
to make it clear that we're working with just the product.

4:13
Switch back over and refresh.

4:15
So just 101.

4:17
So now product is equal to product plus one.

4:21
Makes sense.

4:22
Now how can we do this with PHP an operator for auto incrementing.

4:28
Well just simply say product and then plus plus.

4:32
Let's save that and it should give us the same result.

4:37
So, if we do the opposite of that, , it will autodecrement.

4:44
So refresh, 99.

4:46
So, ++ and  are two more operators in PHP for arithmetic.

4:54
One for autoincrement and one for autodecrement.
You need to sign up for Treehouse in order to download course files.
Sign up