Locked lesson.
About this lesson
Logic sits at the core of all computer programming. Understanding logic operators in PHP.
Exercise files
Download this lesson’s related exercise files.
Logic Operators.docx58.8 KB Logic Operators - Solution.docx
59 KB
Quick reference
Logic Operators
Logic operators allow us to do logic (and/or).
When to use
Whenever you need to compare more than one thing, use logic operators (and/or).
Instructions
And (&&), Or (||), xor, allow us to compare more than one thing.
and (&&) both true to be true, otherwise false
or (||) one true to be true, otherwise false
xor only one can be true but not both
Hints & tips
- And (&&)
- Or (||)
- xor
- 00:04 In the last video, we talked about increment operators.
- 00:07 In this video, I wanna talk about logic operators.
- 00:10 Logic operators go with conditional statements and comparison operators.
- 00:14 We haven't learned about conditional statements yet, so
- 00:16 it's kind of hard to see these in action.
- 00:18 In just a couple videos from now, we're gonna talk about conditional statements,
- 00:22 and then we'll be able to use all of these in more concrete ways.
- 00:24 So what's a logic operator?
- 00:25 When it comes to programming most of the time, your logic is gonna be and and or.
- 00:31 Let's just kind of play around with summaries.
- 00:33 Is 5 greater than 1?
- 00:35 Yes, it is.
- 00:37 And is 10 > 2, this is not
- 00:42 a real PHP statement, but to use and we actually just type the word and, or
- 00:47 we can also use this && sign, either of them will work with PHP.
- 00:53 So I'm just gonna type in and for now.
- 00:54 In order for this to evaluate true, both of these conditions have to be true.
- 00:59 This has to be true and this has to be true.
- 01:03 So, is it?
- 01:03 Is 5 > 1?
- 01:05 Yes. Is 10 > 2?
- 01:06 Yes, this is true.
- 01:09 Is 5 > 1 and 10 > 100?
- 01:14 5 is yes, greater then one and is 10 greater than a 100,
- 01:19 no 10 is not so this is false.
- 01:22 That's and, likewise, or
- 01:25 is 5 greater then 1 or 10 greater than 100.
- 01:32 For this to be true, either this one or this one has to be true.
- 01:38 If either of these are true, then it evaluates to true.
- 01:42 If both of them are true, it also evaluates to true, it's both or one or
- 01:46 the other.
- 01:47 So 5 > 1.
- 01:48 Yes.
- 01:49 10 > 100?
- 01:50 No.
- 01:51 This is false, but this is true.
- 01:53 And since only one of these has to be true, this evaluates to true.
- 01:58 Likewise, is 5 greater than 10, or 10 greater than 100?
- 02:04 5 is not greater than 10, so that's false.
- 02:06 10 is not greater than 100, so that's false.
- 02:09 Neither one of these is true.
- 02:11 So this whole statement evaluates to false.
- 02:14 And you can do this, double && sign.
- 02:18 That's the same thing, same thing for or, we can do those double pipe sign.
- 02:23 Either of these will work.
- 02:25 Those are logical operators.
- 02:26 There' s actually one more, it's called x or and for x or
- 02:30 to be true either those has to be true, but not both.
- 02:33 So if 5 > 10, x or
- 02:38 10 > 100, is this true?
- 02:44 5 is not greater than 10.
- 02:46 10 is not greater than 100 and neither of these is true, so this is false.
- 02:51 Let's try a different one.
- 02:52 Is 5 > 1x or 10 > 100.
- 02:57 Well, 5 is greater than 1.
- 02:59 10 is not greater than 100.
- 03:00 One of these has to be true, but the other one can't be true.
- 03:04 So, this is true.
- 03:05 And finally, one more example, is 5 > 1x or 10 > 1.
- 03:12 Well, 5 is greater than 1, that's true.
- 03:14 10 is greater than 1, that's true, but the statement is false.
- 03:19 Why? Because for x or
- 03:20 to be true, one of them has to be true, but the other one can't be true.
- 03:24 So only one of them can be true for the whole statement to evaluate true.
- 03:28 If your head is spinning don't worry about it.
- 03:30 You're not going to use x or very much.
- 03:31 These are just general things.
- 03:33 These are not actually how you use these in PHP.
- 03:35 We'll learn how to use them once we learn conditional statements.
- 03:38 A couple of videos from now, but this is the general logic behind it, and and or.
- 03:42 Very popular, cuz a lot of times you wanna evaluate two different things.
- 03:46 You wanna say is 5 > 1 and is 10 > 2.
- 03:49 When that's the case, you'll use these logic operators.
- 03:52 In the next video, we'll look at string operators.
- 03:54 That'll be our last operator to look at and we'll move on from there.
Lesson notes are only available for subscribers.