1 00:00:00,750 --> 00:00:03,320 One of the goals we should have when writing code 2 00:00:03,320 --> 00:00:06,890 is to clearly define what our code should be doing. 3 00:00:06,890 --> 00:00:11,540 In light of this goal, sometimes we don't need to compare if something is equal, but 4 00:00:11,540 --> 00:00:15,330 rather we need to compare if something is not equal. 5 00:00:15,330 --> 00:00:18,410 As an example, if a user is not logged in, 6 00:00:18,410 --> 00:00:20,750 we want to redirect them to a login screen. 7 00:00:21,750 --> 00:00:25,740 We check for nonequality by using negation operators. 8 00:00:25,740 --> 00:00:29,540 Let's not just talk about them, let's start using these negation operators. 9 00:00:31,010 --> 00:00:34,560 There are many different ways to check for nonequality. 10 00:00:34,560 --> 00:00:37,900 We've actually already done this with our first else statement. 11 00:00:37,900 --> 00:00:41,310 Let's comment out our code from score and add the first else statement. 12 00:00:48,409 --> 00:00:54,380 If ($a == $b), 13 00:00:54,380 --> 00:01:02,345 echo 'values are equal'. 14 00:01:02,345 --> 00:01:08,750 Else echo 'values are NOT equal'. 15 00:01:13,073 --> 00:01:16,835 If we only want to do something if an expression is not true, 16 00:01:16,835 --> 00:01:21,550 such as redirect to a login in page if the user is not logged in. 17 00:01:21,550 --> 00:01:23,790 We can just leave off the action in the first block. 18 00:01:26,440 --> 00:01:29,800 This can be confusing, hard to read and extra code. 19 00:01:29,800 --> 00:01:34,090 We could also compare an expression to false. 20 00:01:35,750 --> 00:01:41,697 Let's duplicate this and add parentheses and 21 00:01:41,697 --> 00:01:46,430 a condition and then say == false. 22 00:01:49,075 --> 00:01:51,490 Now we can remove this else. 23 00:01:51,490 --> 00:01:56,180 This at least removes our need for an empty code block and the extra else, but 24 00:01:56,180 --> 00:01:59,310 it hasn't made the code really any easier to read. 25 00:01:59,310 --> 00:02:02,170 If anything, it's made it more complicated. 26 00:02:02,170 --> 00:02:05,470 This is where a negation operators can clarify your code and 27 00:02:05,470 --> 00:02:06,680 make it easier to read. 28 00:02:07,780 --> 00:02:09,110 Let's duplicate this line. 29 00:02:13,784 --> 00:02:14,910 And this time. 30 00:02:19,031 --> 00:02:22,341 We'll use a negation operator. 31 00:02:22,341 --> 00:02:26,858 To start with, if you want to check if something is less than or greater than 32 00:02:26,858 --> 00:02:31,398 a value, you can combine those two operators, less than or greater than. 33 00:02:31,398 --> 00:02:36,380 Essentially this tests if a is not equal to b, 34 00:02:36,380 --> 00:02:40,640 because a is either less than or greater than b. 35 00:02:40,640 --> 00:02:42,150 Let's run this in the console again. 36 00:02:45,550 --> 00:02:47,350 We see that the values are not equal. 37 00:02:47,350 --> 00:02:48,600 The values are not equal. 38 00:02:48,600 --> 00:02:51,420 Values are not equal three times. 39 00:02:51,420 --> 00:02:55,690 Less than or greater than doesn't make sense when comparing strings. 40 00:02:55,690 --> 00:03:00,710 Fortunately PHP also provides a negation operator, the exclamation point. 41 00:03:01,850 --> 00:03:05,570 This is typically what you'll see used when checking for a negative value, 42 00:03:05,570 --> 00:03:08,280 because it works no matter which type of values you're comparing. 43 00:03:09,310 --> 00:03:14,310 Let's create a new conditional, and comment out the rest of these. 44 00:03:20,756 --> 00:03:25,200 Okay, if a is not equal to b. 45 00:03:26,385 --> 00:03:29,860 Now we can run the script, and we see that the values are not equal. 46 00:03:31,220 --> 00:03:34,890 Notice I use a single equal after the exclamation point. 47 00:03:34,890 --> 00:03:39,000 This means that the values of a and b are not equal. 48 00:03:39,000 --> 00:03:42,920 This does not compare the variable types, just the values. 49 00:03:42,920 --> 00:03:51,180 So if I change the value of a to be the string five, and run the script again, 50 00:03:52,880 --> 00:03:57,860 I get no output because the string five is still equal to the integer five. 51 00:03:59,250 --> 00:04:00,420 If I wanted to check for 52 00:04:00,420 --> 00:04:05,030 the type as well, I can use a double equal after the exclamation point. 53 00:04:09,969 --> 00:04:12,320 Let's change this to not identical. 54 00:04:18,375 --> 00:04:24,207 Now when I run the script, I see NOT identical because 55 00:04:24,207 --> 00:04:29,770 the string a is not identical to the integer a. 56 00:04:29,770 --> 00:04:31,820 They're of different types. 57 00:04:31,820 --> 00:04:34,669 Let's comment out these conditionals and try something else. 58 00:04:36,370 --> 00:04:42,590 Conditional statements evaluate the value of the given data as either true or false. 59 00:04:42,590 --> 00:04:47,510 In these examples, we've been comparing two variables as either true or false. 60 00:04:47,510 --> 00:04:52,550 But any single variable or value can be evaluated as true or false as well. 61 00:04:53,600 --> 00:04:57,580 I'll add the link in the teacher's notes, where we explore this in more depth. 62 00:04:57,580 --> 00:05:04,888 For now, let's just say if a echo true. 63 00:05:04,888 --> 00:05:10,750 When we run the script, we see true. 64 00:05:10,750 --> 00:05:14,190 Because the value 5 evaluates to true. 65 00:05:14,190 --> 00:05:20,080 If I change the value of a to equal 0 and 66 00:05:20,080 --> 00:05:23,850 run the script again, we no longer see true, 67 00:05:23,850 --> 00:05:29,491 because like most programming languages PHP treats the integer 0 as false. 68 00:05:30,580 --> 00:05:32,170 When I duplicate this statement. 69 00:05:34,995 --> 00:05:40,670 And change it to NOT a, this would be false. 70 00:05:43,654 --> 00:05:50,490 Now I can run my statement and see false, because a does not evaluate to true. 71 00:05:50,490 --> 00:05:55,680 A evaluates to false, which is the value we're looking for in our conditional. 72 00:05:55,680 --> 00:05:57,050 I could also write this another way. 73 00:05:58,973 --> 00:06:03,779 I could do a is equal 74 00:06:03,779 --> 00:06:08,290 to false. Or even 75 00:06:11,050 --> 00:06:15,990 A is not equal to true. 76 00:06:15,990 --> 00:06:22,410 Now when I run the script, I see false three times, 77 00:06:23,580 --> 00:06:28,930 because I'm comparing the value of a to the boolean true or false, 78 00:06:28,930 --> 00:06:33,530 PHP juggles the value of a to b, either true or false. 79 00:06:33,530 --> 00:06:36,130 You can see more about type juggling in the teacher's notes. 80 00:06:37,710 --> 00:06:42,420 In this video, I demonstrated many different ways to get the same answer. 81 00:06:42,420 --> 00:06:46,140 You should choose the option that most clearly expresses your intent. 82 00:06:46,140 --> 00:06:49,100 You'll hear me repeat this many times. 83 00:06:49,100 --> 00:06:53,840 Making your code easy to read is a great benefit for others and for 84 00:06:53,840 --> 00:06:55,390 your future self. 85 00:06:55,390 --> 00:07:00,533 It's been estimated that 80% of the developers' time is spent reading code. 86 00:07:00,533 --> 00:07:04,697 So code that's easy to read helps you be more productive and 87 00:07:04,697 --> 00:07:07,289 makes your day much more enjoyable. 88 00:07:07,289 --> 00:07:10,870 Choose operators that clearly model your requirements. 89 00:07:10,870 --> 00:07:15,930 If you need to check if a user is not logged in, use a negation operator. 90 00:07:15,930 --> 00:07:19,000 If you're looking for score of 60 or greater, 91 00:07:19,000 --> 00:07:24,290 don't just use greater than 59, use greater than or equal to 60. 92 00:07:24,290 --> 00:07:29,730 If you need to compare the variables as identical and not just equal in value, 93 00:07:29,730 --> 00:07:34,650 meaning that the types match as well as the values, use the triple equal. 94 00:07:34,650 --> 00:07:38,940 By clearly expressing the requirements of your program, you make your conditionals 95 00:07:38,940 --> 00:07:43,170 and thereby your code as a whole easier and more enjoyable to read.