1 00:00:01,630 --> 00:00:04,440 PHP7 also brings us some new operators. 2 00:00:04,440 --> 00:00:08,210 The first one we're going to explore is the space ship operator. 3 00:00:08,210 --> 00:00:11,340 With a name like that, who doesn't want to use it? 4 00:00:11,340 --> 00:00:16,540 The space ship operator, or combined comparison operator, is a nice addition 5 00:00:16,540 --> 00:00:21,420 to the language, complementing the greater than and the less than operators. 6 00:00:21,420 --> 00:00:26,270 The spaceship operator is put together using three individual operators 7 00:00:26,270 --> 00:00:29,350 less than, equal and greater than. 8 00:00:29,350 --> 00:00:33,710 Essentially what it does is check each operator individually. 9 00:00:33,710 --> 00:00:35,350 First, less than. 10 00:00:35,350 --> 00:00:39,470 If the value of the left is less than a value on the right. 11 00:00:39,470 --> 00:00:42,740 The Spaceship Operator will return minus 1. 12 00:00:42,740 --> 00:00:43,730 If not, 13 00:00:43,730 --> 00:00:50,300 it will move to test if the value on the left is equal to the value on the right. 14 00:00:50,300 --> 00:00:52,430 If so, it will return 0. 15 00:00:52,430 --> 00:00:56,890 If not, it will move to the final test, 16 00:00:56,890 --> 00:01:01,720 if the value on the left is greater than the value on the right. 17 00:01:01,720 --> 00:01:06,330 Which if the other two haven't passed this one must be true and it will return 1. 18 00:01:06,330 --> 00:01:10,350 The most common uses for this operator is in sorting. 19 00:01:11,410 --> 00:01:16,540 Another new operator the null coalesce operator is effectively the fabled if, 20 00:01:16,540 --> 00:01:17,930 set, or. 21 00:01:17,930 --> 00:01:22,080 It will return the left operand if it is not null. 22 00:01:22,080 --> 00:01:24,700 Otherwise it will return the right. 23 00:01:24,700 --> 00:01:29,520 The important thing is that it will not raise unnoticed if the left operand 24 00:01:29,520 --> 00:01:31,880 is a nonexistence variable. 25 00:01:31,880 --> 00:01:33,230 For example, 26 00:01:33,230 --> 00:01:39,360 name equals the variable first name double question marks the string Guest. 27 00:01:39,360 --> 00:01:44,030 If the variable first name is Seth and it is not null. 28 00:01:44,030 --> 00:01:48,020 It will assign that value to the variable name or 29 00:01:48,020 --> 00:01:51,160 else it will assign Guest to the variable name. 30 00:01:52,210 --> 00:01:56,360 Before PHP7, you could write something like this. 31 00:01:56,360 --> 00:02:02,270 If not empty first name, name equals first name else name equals Guest. 32 00:02:03,500 --> 00:02:07,810 What makes this even more powerful, is that you can stack these. 33 00:02:07,810 --> 00:02:11,800 This operation will check each item from left to right. 34 00:02:11,800 --> 00:02:15,640 And when it finds one that is not null, it will use that value. 35 00:02:16,690 --> 00:02:21,320 This operator looks explicitly for null, or does not exist. 36 00:02:21,320 --> 00:02:23,120 It will pick up an empty string.