Negation Operator7:43 with Alena Holligan
One of the goals we should have when writing code, is to clearly define what our code should be doing. In light of this goal, sometimes we don't need to compare if something is equal but rather we need to compare if something is NOT equal. In this video, we'll look at different ways to check for NON-equality.
One of the goals we should have when writing code 0:00 is to clearly define what our code should be doing. 0:03 In light of this goal, sometimes we don't need to compare if something is equal, but 0:06 rather we need to compare if something is not equal. 0:11 As an example, if a user is not logged in, 0:15 we want to redirect them to a login screen. 0:18 We check for nonequality by using negation operators. 0:21 Let's not just talk about them, let's start using these negation operators. 0:25 There are many different ways to check for nonequality. 0:31 We've actually already done this with our first else statement. 0:34 Let's comment out our code from score and add the first else statement. 0:37 If ($a == $b), 0:48 echo 'values are equal'. 0:54 Else echo 'values are NOT equal'. 1:02 If we only want to do something if an expression is not true, 1:13 such as redirect to a login in page if the user is not logged in. 1:16 We can just leave off the action in the first block. 1:21 This can be confusing, hard to read and extra code. 1:26 We could also compare an expression to false. 1:29 Let's duplicate this and add parentheses and 1:35 a condition and then say == false. 1:41 Now we can remove this else. 1:49 This at least removes our need for an empty code block and the extra else, but 1:51 it hasn't made the code really any easier to read. 1:56 If anything, it's made it more complicated. 1:59 This is where a negation operators can clarify your code and 2:02 make it easier to read. 2:05 Let's duplicate this line. 2:07 And this time. 2:13 We'll use a negation operator. 2:19 To start with, if you want to check if something is less than or greater than 2:22 a value, you can combine those two operators, less than or greater than. 2:26 Essentially this tests if a is not equal to b, 2:31 because a is either less than or greater than b. 2:36 Let's run this in the console again. 2:40 We see that the values are not equal. 2:45 The values are not equal. 2:47 Values are not equal three times. 2:48 Less than or greater than doesn't make sense when comparing strings. 2:51 Fortunately PHP also provides a negation operator, the exclamation point. 2:55 This is typically what you'll see used when checking for a negative value, 3:01 because it works no matter which type of values you're comparing. 3:05 Let's create a new conditional, and comment out the rest of these. 3:09 Okay, if a is not equal to b. 3:20 Now we can run the script, and we see that the values are not equal. 3:26 Notice I use a single equal after the exclamation point. 3:31 This means that the values of a and b are not equal. 3:34 This does not compare the variable types, just the values. 3:39 So if I change the value of a to be the string five, and run the script again, 3:42 I get no output because the string five is still equal to the integer five. 3:52 If I wanted to check for 3:59 the type as well, I can use a double equal after the exclamation point. 4:00 Let's change this to not identical. 4:09 Now when I run the script, I see NOT identical because 4:18 the string a is not identical to the integer a. 4:24 They're of different types. 4:29 Let's comment out these conditionals and try something else. 4:31 Conditional statements evaluate the value of the given data as either true or false. 4:36 In these examples, we've been comparing two variables as either true or false. 4:42 But any single variable or value can be evaluated as true or false as well. 4:47 I'll add the link in the teacher's notes, where we explore this in more depth. 4:53 For now, let's just say if a echo true. 4:57 When we run the script, we see true. 5:04 Because the value 5 evaluates to true. 5:10 If I change the value of a to equal 0 and 5:14 run the script again, we no longer see true, 5:20 because like most programming languages PHP treats the integer 0 as false. 5:23 When I duplicate this statement. 5:30 And change it to NOT a, this would be false. 5:34 Now I can run my statement and see false, because a does not evaluate to true. 5:43 A evaluates to false, which is the value we're looking for in our conditional. 5:50 I could also write this another way. 5:55 I could do a is equal 5:58 to false. Or even 6:03 A is not equal to true. 6:11 Now when I run the script, I see false three times, 6:15 because I'm comparing the value of a to the boolean true or false, 6:23 PHP juggles the value of a to b, either true or false. 6:28 You can see more about type juggling in the teacher's notes. 6:33 In this video, I demonstrated many different ways to get the same answer. 6:37 You should choose the option that most clearly expresses your intent. 6:42 You'll hear me repeat this many times. 6:46 Making your code easy to read is a great benefit for others and for 6:49 your future self. 6:53 It's been estimated that 80% of the developers' time is spent reading code. 6:55 So code that's easy to read helps you be more productive and 7:00 makes your day much more enjoyable. 7:04 Choose operators that clearly model your requirements. 7:07 If you need to check if a user is not logged in, use a negation operator. 7:10 If you're looking for score of 60 or greater, 7:15 don't just use greater than 59, use greater than or equal to 60. 7:19 If you need to compare the variables as identical and not just equal in value, 7:24 meaning that the types match as well as the values, use the triple equal. 7:29 By clearly expressing the requirements of your program, you make your conditionals 7:34 and thereby your code as a whole easier and more enjoyable to read. 7:38
You need to sign up for Treehouse in order to download course files.Sign up