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
Comparison operators allow us to compare values and yield the result. In this video we'll be looking at equal and identical comparisons. Equal compares ONLY the values while identical compares the values and type. We'll also consider type juggling in PHP.
Example
To assign the result of a comparison to another value, you would use:
$a = 10;
$b = "10";
$c = ($a == $b);
It is a good idea to use parentheses to be clear on evaluation.
Documentation
Learn about additional Comparison Operators
An operator is something that takes 1 or
more values and yields another value.
0:00
I've shown you several
different operators so far.
0:05
The assignment operator uses the equal
sign to put a value into a variable.
0:08
The arithmetic operators let you add,
subtract, divide, and multiply numbers.
0:14
And the concatenation operators
let you create a new string
0:19
by combining several strings together.
0:23
Next we'll be looking at
comparison operators for equality.
0:26
Comparison operators do
exactly what they sound like.
0:31
They allow us to compare values and
yield the result.
0:34
There are quite a few
comparison operators, and
0:38
we'll cover them all in a later course.
0:40
I have included more information
in the teacher's notes,
0:42
if you want to learn more now.
0:44
For this course, we'll be looking
at equal and identical comparisons.
0:46
Equal compares only the value, while
identical compares the value and the type.
0:51
We've looked at the number of different
variable types in PHP including
0:57
integers floats strings and booleans.
1:01
PHP is called a weakly or
loosely typed language.
1:04
This means that, generally, you don't
need to specify the type you'll be using.
1:08
PHP will automatically assign the type for
1:13
you based on the value
inside the variable.
1:15
This also means that PHP will attempt
1:19
to juggle the types to work within
the situation in which they're being used.
1:22
We saw this when working
with number variables.
1:27
When you were using the plus
sign to add two variables
1:30
PHP assumes that those values
should be evaluated as numbers.
1:33
Type juggling can be
helpful when accepting data
1:38
from sources beyond your control
like user data from a web form.
1:41
Values submitted from an online
form are collected as a string, but
1:45
they can be used in a calculation
because of Type Juggling.
1:49
Let's comment out
1:54
these output lines.
1:59
Then let's start with something simple.
2:04
We'll add var_dump(1 + "2").
2:06
Now let's run our script.
2:14
Since we're using an addition operator,
2:16
when we run our program PHP juggles
the string 2 to an integer and
2:20
adds 1 + 2, which gives us
the integer 3 as the result.
2:26
So now let's take a look
at the comparison operator.
2:31
We'll start by creating two variables,
2:34
a = 10 and b = 10.
2:39
Then we can compare these
variables as equal and
2:43
use our var_dump to display the results.
2:46
When we run the script,
2:57
we see that the evaluation is true
because both values equal 10.
2:59
If we want to compare if these
variables are identical, meaning that
3:03
the value is the same and also the type
is the same we use the triple equals.
3:08
When we run the script now,
we see that the evaluation is false,
3:20
because a and b are not the same type.
3:24
We can also use these to compare strings.
3:28
We'll var_dump again, and
3:31
then we'll compare string_one
3:36
to be equal to learning to display
3:41
Hello Alena to the screen.
3:46
Let's uncomment this line
to echo out our string, so
3:54
we can see what that looks like.
3:58
Great.
4:03
Well, let's also comment
about these //var_dumps.
4:05
There we go.
4:11
That should make our output
a little easier to read.
4:11
Our result of string one
isn't exactly the same,
4:16
because we have I am in front,
and we also have a new line.
4:19
So the VAR dump evaluates
to the bullion false.
4:25
Let's update the string one value and
remove the new line and
4:27
prepend string using our comments.
4:32
Not this or this.
4:37
Now let's run our script again.
4:42
Great, now our var dump evaluates to true.
4:44
Even though string one uses the name
variable, the values are the same.
4:47
You need to sign up for Treehouse in order to download course files.
Sign up