Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Over the course of the next two movies, we're going to be talking about numbers. And we're going to start out by first talking about Integers. Integers are whole numbers, so that's numbers like 1, 2, 3, 4, 5, as well as negative 1, negative 2, negative 3, and so on. I think what a number is, is pretty intuitive. We all kind of have an idea of that by now. But we do still need to see how we work with them in PHP. Let's create a new document we can work with. I'm going to open up basic.HTML and I'm going to do just Save As. I'm going to change this one to be integers.PHP. Make sure you've got PHP at the end. Integers.
Now, we've seen how we could just have our basic PHP tags and assign a variable. var1 equals 3, and var2 equals 4. And we saw how we can add those together and we can echo back the result. Let me go ahead and just show you a bit more math. We're going to be echoing the result back from adding 1 plus 2 plus the value of var1. That's 3, and then multiplying that by var2. This asterisk is what we use for multiplying when we're programming. And then, the result of all of that in parentheses is going to be divided by, that's what the forward slash here means, divided by 2 minus 5.
Now, the parentheses, the order of operations between the multiplication and division and all that, it's going to still apply. It's just basic math that follows the basic math rules. So, let's try that out. Let's open that up in a browser. Instead of string functions, we're going to be looking for integers. And there it is, basic math, the answer is 7. So, it added all of those together and did a calculation for us. Let me show you a few other math things that we can do, and that's to use some functions. So, here's if you want to find absolute value, you use the abs function.
So, 0 minus 300, that will, of course, return the absolute value, which would be 300 instead of negative 300. Exponential, raising something to the power of something. The function we're going to use is pow. So, you see the arguments are 2 and 8, so that's 2 to the 8th power, and then we have the square root, sqrt. The square root of 100, and then fmod is for modulo. Now, if you've never worked for modulo before, what it's going to do is it's going to take 20 and divide it by 7. And return just the remainder to me. What's left over from that, things don't divide evenly, it will tell us. That can be very handy for finding out whether one number divides evenly into another number.
And then rand, of course, will return a random number to us. And rand with a minimum and maximum value will return a random number within that range. Let's try all of those out. Save it. Back, and here you go. And you can see the random number it gave me was a really large number. Whereas the random min max it gave me was between 1 and 10. If we reload that page, you'll see that it gives me different values for each of those this time around. And you can see what I was also talking about with modulo. We were dividing 20 by 7, 20 by 7, will divide into it twice, with 6 left over.
So, the modulo is the remainder that's left after that. So, we could know that it does not divide evenly. In addition to these functions, I want to also show you how to increment and decrement the numbers a little bit. Let's do a new row here and let's, first, I want to show you how to do plus equals. So, we'll do some PHP, and we're going to say var2 plus equals 4. Now, what that's going to do is it's going to update variable 2 in place by adding 4 to it. It's the same way we were doing that concatenation in assignment at the same time when we were working with strings. And if the exact same thing as if we had actually typed out var2 equals var2 plus 4, it just saves us some typing to write it this way. Let's echo that value back just so we can see what it is, var2, we'll put a br tag at the end.
Now, not only is there the plus version of this, but there's a minus version, a multiplication version and a division version. So ,we can do all of those as well. And just so they aren't all exactly the same, I'm going to change this four to a three. So, that'll multiply it by 3 and then divide it by 4. Let's try this out real quick. Save the document. Switch back. We'll reload the page. And you can see they took the last value. It starts out with a value of four It adds four to it to get 8, which subtracts four to get 4. Multiplies by 3 to get 12, and then divides by 4 to get 3.
Now, incrementing and decrementing by one is super common, especially when we start working with loops. I'll just show you how that works, so we're going to call this increments. And if we want to increment plus equals 1, we could just do it like that. There is nothing wrong with that and it might actually be very clear what we're doing. But it's so common that we could just do it as var2++. That just mean adds one to it. Increment it. This actually comes from the world of C. C uses this when we're incrementing loops.
It's just a nice handy short cut. We can do the same thing with decrementing with minus, minus. So, if you're going through a loop and you want to keep a variable incremented. Every time you go through the loop to count your iterations through the loop, you can do that by using this plus, plus or minus, minus. And we could try it out real quick. As you would expect, it comes up and tells us that it went from 3 to 4 and 4 back down to 3. The last thing that I want to show you about integers before we move on to floating point numbers. Is I just want to make sure that it's clear to you that there is a difference between the number 1 and the string 1, right? Those are not the same thing. This is just a character.
It could might as well be x or y or z. It's just a character on the screen, it's not a number that's ready to be added. Now, in truth, PHP will do its best job to add it together. If we did something like this, PHP will say, all right, I realize this is not a number. So, it's not suitable for adding, but I wonder if I can convert it. I wonder if I can change it into an integer so that I can complete the operation. And it will do that. Let's just see that real quick, and you see it came up with two. But if I said this is 1 plus 2 houses, usually it comes up with 3. The word houses just disappears because it converts it. And the best way it can convert it to an integer is to pick the number that it see out of it.
And throw the rest of it away. And that's the only way that it knows how to. In general, you should not rely on PHP to convert strings into integers for you. You should be working with one or the other, and you should intentionally switch from one to the other if you really need to. And we'll talk about how to do that later on. But, it's considered sloppy programming to combine types like this without explicitly converting them to the right type.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101284 Viewers
61 Video lessons · 88043 Viewers
71 Video lessons · 71897 Viewers
56 Video lessons · 103729 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.