Locked lesson.
About this lesson
Comparing two or more things is easy with comparison operators.
Exercise files
Download this lesson’s related exercise files.
Comparison Operators.docx58.7 KB Comparison Operators - Solution.docx
58.9 KB
Quick reference
Comparison Operators
Comparison operators allow us to compare things.
When to use
Anytime you need to compare two or more things, use comparison operators.
Instructions
Here's the main list of comparison operators:
== equal to
=== identical
!= not equal
<> not equal
!== not identical
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Hints & tips
- Comparison operators are used to compare things
- 00:05 In the last video we talked about assignment operators, and
- 00:07 in this video I wanna talk about comparison operators.
- 00:10 Comparison operators are incredibly important and so
- 00:13 much of programming is about comparing things.
- 00:16 Is this equal to that?
- 00:17 Is this greater than that?
- 00:19 Is this identical to that?
- 00:20 Later on, a few videos from now, we're gonna actually look at conditional
- 00:24 statements that allow us to actually test two different things
- 00:27 that says if this number is greater than that number, do this, otherwise, do that.
- 00:32 But, in order to do that we need comparison operators.
- 00:35 We need the operators that allows us to ask the question, is this equal?
- 00:40 Is this not equal?
- 00:41 Is this greater than?
- 00:42 That's what we're gonna talk about in this video.
- 00:43 And it's kind of hard to talk about it because until we learn how to
- 00:46 do conditional statement we can't actually see these used in real life.
- 00:50 The first one is kind of tricky,
- 00:52 how do we figure out if something is equal to something else?
- 00:55 How do we figure out if number 1 is equal to number 2,
- 00:57 would we write it like $number1 = $number2.
- 01:02 Or we couldn't because this equal to sign is an assignment operator.
- 01:07 If we wrote this, it would change number 1 from 10 into 5.
- 01:11 This would be saying, hey number 1 equals number 2.
- 01:15 Number 2 is 5, so that means number 1 is now 5.
- 01:18 That's not what we want here.
- 01:19 We want to compare, we don't want to assign.
- 01:22 To do this in PHP, to determine if these are equal or
- 01:25 not, we use a double equal to sign.
- 01:27 That means equal in comparison operator land.
- 01:30 Likewise, if we wanted to see if these are identical,
- 01:33 we would use triple equal to signs.
- 01:36 We wouldn't really do that with numbers because numbers are equal,
- 01:39 they're not identical.
- 01:40 But if you had, for instance, John and Elder and you wanted to see if these
- 01:44 are two identical, well this is obviously not identical, but
- 01:48 this is what you would use to test against that.
- 01:51 Equal identical also important not equal.
- 01:55 And PHP has a couple of ways to do not equal.
- 01:58 Exclamation point equal to, that means not equal, and that also means not equal.
- 02:04 And you can use both of these in different ways and
- 02:06 we'll see how to use them separately later on, but these mean not equal to.
- 02:11 Likewise, this means not identical and actually let's start a list here.
- 02:16 Equal, identical,
- 02:20 not equal, also not equal
- 02:28 Not identical.
- 02:31 Those are the main ones.
- 02:32 Now, these next few are very simple and you're probably already aware of them.
- 02:36 This is greater than, remember back from grade school math.
- 02:40 This is less than.
- 02:42 So this is saying, is this number greater than that number?
- 02:45 For instance, you would say 5 greater than 2, this is true.
- 02:49 5 greater than 10, this's false.
- 02:52 We'll get into using these in a couple of videos from now.
- 02:55 Finally, we have greater than or equal to.
- 02:58 Greater than or equal to.
- 03:01 And of course, less than or equal to.
- 03:03 It's kind of weird to see
- 03:07 these without actually using them but like I said, we'll use these very soon.
- 03:12 These are our comparison operators.
- 03:13 Write them down, remember them.
- 03:15 Equal and identical.
- 03:16 Not equal, not equal, not identical.
- 03:18 Pretty easy to wrap your brain around once you've used them a couple of times.
- 03:22 And of course this, greater than, equal to.
- 03:23 Greater than, less than, or equal to.
- 03:25 And we're almost through with our operator list.
- 03:27 We've got a couple more to do.
- 03:28 In the next video, we're gonna talk about increment operators.
Lesson notes are only available for subscribers.