Locked lesson.
About this lesson
Learn how to do math in PHP.
Exercise files
Download this lesson’s related exercise files.
Arithmetic Operators.docx58.8 KB Arithmetic Operators - Solution.docx
59.1 KB
Quick reference
Arithmetic Operators
We can do math with Arithmetic Operators.
When to use
Anytime you want to do any sort of math, you'll use arithmetic operators.
Instructions
Math operators act just the way you'd think they do.
+ add
- subtract
* multiply
/ divide
** or pow() exponent
% modulus (remainder)
You can use math operators on variables or on the numbers directly themselves:
echo $num1 + $num2;
or
echo 3 + 5;
Hints & tips
- Arithmetic operators allow us to do math.
- They work exactly how you'd think they would.
- 00:05 In the last video, we talked about data types.
- 00:07 In this video I want to talk about math.
- 00:09 Specifically arithmetic operators.
- 00:11 In programing languages, there are a lot of different operators, there's arithmetic
- 00:15 operators, there's assignment operators, comparison operators,
- 00:18 increment operators, logic operators, string operators, and on and on.
- 00:21 We're gonna talk about all those other things in coming videos, but in this one,
- 00:25 we're going to start with arithmetic operators, and that's just math right?
- 00:29 One of our data types, actually 2 of our data types were integers and floats.
- 00:33 And integers are numbers and whenever you want to do something with numbers you're
- 00:37 going to use an arithmetic operator.
- 00:41 Let's create a variable called number.
- 00:43 Set it equal to 41, actually let's call this number 1.
- 00:49 All right? And let's create another one called number
- 00:51 two and set this equal to four.
- 00:55 To do math, it's just like you think it is.
- 00:58 You use plus, minus, multiplication, the little star guy, division, forward slash.
- 01:05 For exponents, 2 to the 4th power,
- 01:07 something like that, double multiplication.
- 01:11 And finally there is the modulus and we'll talk about the modulus in just a minute.
- 01:15 It's pretty important but a modulus is basically just a remainder
- 01:19 if you took 10 divided by 3, 3 goes into 10 three times right?
- 01:23 3 times 3 is nine with 1 remainder,
- 01:26 remember your division in like fourth grade?
- 01:28 Math class when you're a student, 10 divided by 3 is 3 remainder 1.
- 01:32 So this modulus would return one.
- 01:35 Let's spend just a quick minute or two talking about each of these things.
- 01:39 Now to do math in PHP it's pretty simple.
- 01:41 You just go, let's echo out number1 + number2.
- 01:48 If we save this come back here and click Reload, we get 45 which is 41 + 4.
- 01:53 Let's go 5 and 1, here we go.
- 01:58 Change that around, save it, hit reload, boom, 6.
- 02:00 So that's simple addition.
- 02:02 To do subtraction 5- 1.
- 02:07 4.
- 02:08 Multiplication is just the way you would think it is.
- 02:11 5 times 1 is 5.
- 02:14 Division 5 divided by 1.
- 02:18 It's gonna be 5.
- 02:19 And let's go exponents.
- 02:20 So this would be 5 to the first power.
- 02:23 Which is 5 right.
- 02:24 Let's change this to 2.
- 02:26 So now that's gonna be 5 to the second power which should be 25 right.
- 02:31 Now, I actually got an error.
- 02:32 This is interesting.
- 02:33 This exponent operator was changed in a later version of PHP.
- 02:37 It looks like we're running an old version of PHP on our C9 development environment.
- 02:41 So, to use exponents on this thing.
- 02:44 We need to change it a little bit and uses pow cuz we are raising it to the power.
- 02:48 So we say things as number 1, number 2,
- 02:52 that's five to the second power which is 25.
- 02:57 Same basic thing say the difference and finally the modulus, so
- 03:01 let's go number1 modulus
- 03:07 number 2 or saying what's 5 divided by 2?
- 03:11 2 goes into 5 2 times with 1 leftover.
- 03:14 So this should return 1.
- 03:15 So if we save this, hit reload.
- 03:17 Boom, it's 1.
- 03:18 Now let's change this to 10.
- 03:20 What is 10 divided by 2?
- 03:23 It's 5 with 0 remainder.
- 03:25 Right? Cuz 2 goes into 10 five times.
- 03:28 So, we click reload, 0.
- 03:29 So, that's the modulus, we'll play with that a little bit later on.
- 03:32 That's pretty much it.
- 03:33 That's arithmetic operators.
- 03:34 Math is very simple on most computer programming languages.
- 03:38 PHP, it's very simple as we've just seen.
- 03:41 And in the next video we'll talk about assignment operators.
Lesson notes are only available for subscribers.