‫All right guys, in the last video we looked at type casting, type juggling.
‫And we did that by adding two different data types together and to see what type we got in in response.
‫Now while we're on that, I just want to go over the rest of the arithmetic operators.
‫So we're going to go over different types of operators.
‫These are arithmetic operators.
‫And this is going to be pretty simple.
‫But after that I also want to show you a bunch of the built in functions that we have for for math and
‫for numbers that come built in with PHP.
‫All right.
‫So as far as the arithmetic operators, we have addition, subtraction, multiplication, division and
‫modulus or modulo which is the remainder of a division.
‫So down here I just have some HTML.
‫I figured that we could just output whatever we want to see here.
‫So in fact I'm going to have a paragraph with the class of text Dash XL.
‫And then we'll just have our output which will be in a variable called output.
‫And we'll just initialize that up here.
‫So output.
‫We'll set that to null to begin with.
‫And then whatever we want to see the result of we'll go ahead and put in that variable.
‫All right.
‫Now I do want to have just a couple numbers in variables.
‫So we'll say num one equals 20.
‫And let's say num two equals ten.
‫All right.
‫And then we're just going to do some very basic arithmetic.
‫So I'm going to set output.
‫And I want it to show the expression as well.
‫So in double quotes I'm going to say num one plus num two equals.
‫And then we can concatenate the actual expression.
‫So this will actually be parsed.
‫So num one plus num two.
‫And we'll save that and reload.
‫And we get 20 plus ten equals 30.
‫So of course we can do basic addition.
‫Let's copy that down.
‫And let's change this to subtraction.
‫Change this this label or whatever you want to call it to subtraction.
‫Reload.
‫We get 20 minus ten equals ten.
‫All right.
‫If we want to do multiplication we use an asterisk.
‫So we'll go ahead and do that and reload.
‫We get 20 times ten equals 200.
‫And then division.
‫So let's say slash.
‫And we get 20 divided by ten equals to obviously ten goes into 22 times.
‫And then let's do the modulus operator which is a percent symbol.
‫So we'll do it there and here.
‫And that's going to give us zero, because ten goes into 22 times without a remainder.
‫If I were to change the ten to a seven and reload, we're going to get six.
‫And the reason for that is that seven goes into 22 times 214.
‫Right.
‫And then we have a remainder of six from 14 to 20.
‫So the modulus operator can come in handy a lot.
‫You can use it to tell if something is even or odd.
‫And there's a few use cases for it that are pretty common.
‫All right.
‫Now I want to show you something called an assignment operator.
‫So let's come down here and I'm just going to say assignment.
‫Operator.
‫And up here we'll just say.
‫Basic math.
‫So for assignment operators, let's say we have a num three.
‫Set that to ten.
‫And then let's say I want to change num three to be equal to whatever num three is plus plus let's say
‫20.
‫Okay.
‫So if we come down here and set output to num three because that's what I want to see the value of.
‫Come over here and reload.
‫We get 30.
‫Right.
‫Because it's ten to begin with we're setting it to whatever it is plus 20.
‫Well, there's a shorter way to do this using the addition assignment operator.
‫So we could say num three.
‫And instead of equals num three plus 20 we can simply do plus equals.
‫So that's an assignment operator.
‫And then 20.
‫And I'm just going to comment this out so that it starts out as ten.
‫So if I reload we should get the same thing.
‫We get 30.
‫So this and this do the same exact thing.
‫This is just a shorter way of doing it.
‫And we can do this with with all the operations.
‫So if we want to do minus let's comment that out.
‫So it doesn't I always want num three to start as ten.
‫Do that we get negative ten because ten -20 is negative ten.
‫We can do.
‫Say slash division.
‫Then we get 0.5.
‫Actually, let's do something smaller.
‫We'll do two.
‫So reload.
‫Then we get five.
‫So we're saying num three equals num three which is ten divided by two which is five.
‫All right.
‫So that's those are assignment operators.
‫Now I want to look at just some built in built in PHP functions that come in handy when dealing with
‫numbers and math.
‫So the first one we're going to look at is Rand which will generate a random number.
‫So let's set output equal to just rand.
‫We won't pass anything in yet.
‫And that will give us whoops.
‫We got to get rid of this.
‫So that will give us just a random number.
‫Now the max that it can be, you can actually get with the get.
‫What is it?
‫I think it's get Rand.
‫I haven't used this in a long time, but let's set output to get.
‫Right there.
‫Get Rand max, which is all lowercase.
‫This is an older function, so it doesn't adhere to the underscores.
‫So this is the max.
‫So basically when you call Rand by itself with nothing inside you're going to get anywhere from 0 to
‫2.
‫This.
‫Now you can pass in arguments to rand.
‫So let's say output equals rand.
‫And then I'm going to pass in as the first argument I'm going to pass one.
‫And as a second I'm going to pass ten.
‫And you can probably guess what that's going to do.
‫It's going to give me a random number from 1 to 10.
‫So if I keep reloading it'll always be in between 1 and 10.
‫Now, the next couple that I'm going to show you have to do with.
‫We just do that have to do with rounding.
‫So the first one we'll look at is round and it does just that.
‫We'll set output equal to round.
‫So a function is always going to have parentheses whether you're passing in anything or not.
‫So we're going to pass a number in let's say 4.7.
‫And if we reload we get five.
‫Right.
‫If I if I set it to 4.2 reload I'm going to get four.
‫So it rounds to the nearest ten.
‫Now if you want you can always round up or down with the function ceil and floor.
‫So let's look at ceil first.
‫So I'll say output.
‫And I'm going to set that to ceil.
‫And let's do 4.2.
‫And even though if I were to pass 4.2 into round, I get four, if I pass it into seal, it's going
‫to give me five, because it's always going to round up.
‫And then at the same time we have four.
‫So let's do output equals floor.
‫And here we'll do 4.9.
‫And as you probably guessed that's going to round down to four.
‫Okay.
‫If you want to get the square root of something you can use sqrt.
‫So that will give you the square root let's say output.
‫Set that to sqrt.
‫And let's get the square root of 64 which will give us eight.
‫Okay.
‫Now, if you want to use if you have to use Pi for some reason there is a pi function.
‫So output equals.
‫Pie.
‫And reload.
‫That gives us pi.
‫If you want to get the absolute number so we can use abs.
‫It's just stay consistent here with the titles.
‫Comments.
‫So let's say output.
‫Let's set that to absolute.
‫And the absolute number is going to be a positive number of a value.
‫So if I do -4.7 and I want the absolute value it's going to give me 4.7.
‫Now, if we want to get the highest value in a list of numbers or an array of numbers for that matter,
‫we can use max.
‫So let's say max and let's say output equals max.
‫And I'm going to pass in a list of numbers 123 50.
‫If I reload that, that's going to give us the highest one, which is 50.
‫Now you can pass in a list of numbers.
‫So comma separated values or an array.
‫So if I put this in brackets that makes it an array.
‫And for this one I'll change 50 to.
‫Actually you know what I'll put the second one as 25.
‫And if I reload, I get 25.
‫That's the max in that array.
‫And at the same time, we have min.
‫In fact, I can just copy that.
‫And let's change that to men.
‫And then we're going to do.
‫Men comment that out.
‫Okay, we get one.
‫So for this one let's call men.
‫We're passing in an array.
‫Let's do.
‫We'll make this ten, we'll make this seven, and we'll make this five.
‫So if I reload that, we should see five because that's the lowest or the minimum in that array.
‫And then the last one I want to show you is number format, which is a relatively new function.
‫So let's say number format.
‫And I'm going to put in here 1234567.
‫And then .891234.
‫Okay.
‫Now what this does is it formats a number with grouped thousands.
‫So this this is really good with currency where you want commas where they're supposed to be and so
‫on.
‫So I'm going to pass in.
‫Let's look at this the function signature right.
‫So let's see number format takes in a float.
‫And then the the first argument that I'm going to pass in I should say the second one is the number
‫of decimals.
‫And then the next one will be the decimal separator.
‫And then the next one will be the thousand separator.
‫So for the number of decimals let's say two.
‫For the decimal separator has to be a string.
‫I want that to be a dot.
‫And then for the thousand separator we want that to be a comma.
‫So let's go ahead and save that and reload.
‫Oops.
‫We have to.
‫Set our output here.
‫Okay.
‫And then let's reload.
‫And there we go.
‫So we get 1000 or 1,234,567 and $0.19 or whatever this is.
‫Okay.
‫So this this is really, really helpful because before you'd have to create your own custom, you know,
‫thousands function or whatever, add commas to currency, something like that.
‫But this makes it really easy to just format the number the exact way that you want.
‫All right.
‫So those are the main functions I wanted to show you.
‫There are others as well, but you're not really going to use them too much.
‫Most likely in the next video I want to show you some built in string functions.