1 00:00:00,310 --> 00:00:02,760 So let's talk about operators. 2 00:00:02,760 --> 00:00:08,230 An operator is anything that takes one or more values and returns another value. 3 00:00:08,230 --> 00:00:13,380 When we assign a value using an equal sign that was an assignment operator. 4 00:00:13,380 --> 00:00:18,620 When we added numbers using a plus symbol, that's an arithmetic operator. 5 00:00:18,620 --> 00:00:24,820 When we subtracted numbers using the minus symbol that's also an arithmetic operator. 6 00:00:24,820 --> 00:00:26,280 Besides the addition and 7 00:00:26,280 --> 00:00:30,870 subtraction operators, there are a few more operators we're going to use. 8 00:00:30,870 --> 00:00:35,560 So let's define a few variables and get started using some arithmetic operators. 9 00:00:36,890 --> 00:00:39,126 I'll start by defining a few more integers. 10 00:00:39,126 --> 00:00:42,680 I'll add $a equals five, 11 00:00:42,680 --> 00:00:47,170 and then I'll add another one $b equals ten. 12 00:00:50,070 --> 00:00:53,760 We've got two different variables that contain different values. 13 00:00:53,760 --> 00:00:56,800 We already use the plus sign for addition and minus for 14 00:00:56,800 --> 00:01:00,280 subtraction, so let's try a few more operators. 15 00:01:00,280 --> 00:01:03,800 Let's display the product using the multiplication operator, 16 00:01:03,800 --> 00:01:04,936 which is the asterisk. 17 00:01:04,936 --> 00:01:13,360 var_dump $a times $b. 18 00:01:13,360 --> 00:01:14,470 Let's add one more here. 19 00:01:14,470 --> 00:01:15,920 I'll do the quotient. 20 00:01:15,920 --> 00:01:19,520 The quotient uses the division operator, which is the forward slash. 21 00:01:20,910 --> 00:01:24,850 We'll use, var_dump $a divided by $b. 22 00:01:26,190 --> 00:01:27,930 Now let's run our script. 23 00:01:27,930 --> 00:01:33,370 We see that $a multiplied by $b gives us the integer 50. 24 00:01:33,370 --> 00:01:36,790 5 multiplied by 10 equals 50. 25 00:01:36,790 --> 00:01:43,531 We also see that $a divided by $b gives us a float, 0.5. 26 00:01:43,531 --> 00:01:47,631 5 divided by 10 equals 0.5. 27 00:01:47,631 --> 00:01:52,004 So we can see that even though the variables in these equations are integers, 28 00:01:52,004 --> 00:01:55,851 the results will be returned in whichever variable type is needed. 29 00:01:55,851 --> 00:01:59,927 The next operators that I wanna talk about are the incrementing and 30 00:01:59,927 --> 00:02:02,240 decrementing operators. 31 00:02:02,240 --> 00:02:03,580 These are often used within a loop. 32 00:02:03,580 --> 00:02:06,590 I'll be covering loops in a later course, but for 33 00:02:06,590 --> 00:02:10,000 now, let's just say we want to add one to our variable a. 34 00:02:11,610 --> 00:02:17,926 We could do $a equals $a plus 1, 35 00:02:17,926 --> 00:02:23,270 and then do a var_dump. 36 00:02:23,270 --> 00:02:24,170 Now let's run our script. 37 00:02:24,170 --> 00:02:29,280 And we see that $a equals the integer 6. 38 00:02:29,280 --> 00:02:32,710 5 plus 1 equals 6. 39 00:02:32,710 --> 00:02:37,270 We can also do this with PHP's incrementing operator. 40 00:02:37,270 --> 00:02:44,370 I'll duplicate this var_dump line, and then I'll do $a plus plus. 41 00:02:47,090 --> 00:02:48,280 Now let's run our script again. 42 00:02:50,150 --> 00:02:51,930 $a now equals 7. 43 00:02:51,930 --> 00:02:56,110 Using plus plus is the same as using plus 1. 44 00:02:56,110 --> 00:02:58,300 Six plus one equals seven. 45 00:02:59,410 --> 00:03:01,780 The opposite of that is a decrementing operator. 46 00:03:02,930 --> 00:03:12,244 If I duplicate the var dump once more I can add a minus minus, 47 00:03:12,244 --> 00:03:17,972 now when I run the script, $a equals 6 again. 48 00:03:17,972 --> 00:03:20,732 7 minus 1 equals 6. 49 00:03:20,732 --> 00:03:24,892 We can use these operators before or after the variable. 50 00:03:24,892 --> 00:03:28,392 The end result is the same, but they work slightly different. 51 00:03:28,392 --> 00:03:32,472 If we put the assignment within a var_dump, you can see what's happening. 52 00:03:45,034 --> 00:03:50,338 Now when we run our script the second from the last equals 6, 53 00:03:50,338 --> 00:03:53,364 but the next dump evaluates as 5. 54 00:03:54,530 --> 00:03:58,770 If I add the operator before the variable it will first subtract and 55 00:03:58,770 --> 00:03:59,740 then return the value. 56 00:04:01,020 --> 00:04:06,350 Let's do minus minus $a and save the file. 57 00:04:08,500 --> 00:04:11,120 So the last two dumps both equal four. 58 00:04:12,680 --> 00:04:15,920 There's one last set of operators that we should talk about before moving on. 59 00:04:16,930 --> 00:04:22,100 If we want to add or subtract more than one at a time, we do have another option. 60 00:04:22,100 --> 00:04:27,250 There are combined operators that combine assignment with arithmetic operators. 61 00:04:27,250 --> 00:04:31,650 For example, if we want to increase the value of $a by five, 62 00:04:32,790 --> 00:04:38,340 we could do $a equals $a plus 5. 63 00:04:38,340 --> 00:04:44,340 Then we var_dump our $a. 64 00:04:44,340 --> 00:04:49,590 Now we can run the script, and we can see that $a now equals 9. 65 00:04:49,590 --> 00:04:54,320 4 plus 5 equals 9. 66 00:04:54,320 --> 00:04:57,688 We can also combine those operators for a shorter version. 67 00:04:57,688 --> 00:05:01,514 Let's do. 68 00:05:01,514 --> 00:05:03,510 $a plus equals five. 69 00:05:06,580 --> 00:05:11,850 Now when we run our script, we can see that $a equals fourteen. 70 00:05:11,850 --> 00:05:15,540 9 plus 5 equals 14. 71 00:05:15,540 --> 00:05:19,520 These combined operators work with any of their arithmetic operators, 72 00:05:19,520 --> 00:05:22,720 addition, subtraction, multiplication and division. 73 00:05:24,190 --> 00:05:27,660 That was a lot of numbers and a lot of different operators. 74 00:05:27,660 --> 00:05:32,010 Feel free to re-watch the video to review anything you may have missed. 75 00:05:32,010 --> 00:05:37,150 Most importantly, make sure you try these operations out for yourself. 76 00:05:37,150 --> 00:05:38,330 You'll understand and 77 00:05:38,330 --> 00:05:42,300 remember the coding concepts much more, if you've written the code yourself.