Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we've taken a look at integers, I'm going to to take a look at another type of number which are floating point numbers also simply called Floats, for short. You may know them more commonly as decimal numbers. That is numbers that have a decimals in them followed by a number of significant digits 2.75 is an example of a floating point number. Now, it may seem arbitrary to you if you haven't done a lot of programming before that we divide numbers into these two types. Integers and floating point, and the reason why is because computers store integers and floating points in different ways in memory.
Mostly because of those significant digits that we have to keep track of with floating points. Because of that, almost every single programming language has two different types to work with integers and floating point numbers. So let's look at how PHP works with floats. So, I'm going to start by opening up basic.html, and then I'll just do a Save As on that. And let's just change this to be floats.php and save it in our sandbox. We'll call this floating point numbers. And let's create our first floating point number here. So, inside the PHP tags, I'm going to have echo $float equals 3.14.
That is an example of a floating point number. And floating points interact just fine with our integers. So for example, if we have our $float plus 7, we'll echo that back Let's put some br tags here just so they get them each on new lines, and we can also divide two integers together in order to get floating point numbers. So 4 divided by 3 doesn't divide evenly, so as a result we end up with a floating point number. Let's try those out. So I'll switch back over to browser and we'll go to floats, so there we go, you see that it echoes the first value, 3.14, it adds seven, an integer to it, and gives you a result which is also a floating point number.
Doesn't matter that we have two different types, it handles them, they're both numeric, and so it knows that they can be added together. And the result might be an integer or the result might be a float. One important point that I want to make for you, is just that we can't divide by zero. Just be aware of that. It does not give us any kind of a floating point number or anything. It's an illegal operation, and we'll get a warning if we try and divide four by zero. Let's just try that real quick so you can see. Comes up and it gives us a warning, says oops, there's a warning, can't divide by zero. Once we learn how to do conditional statements, we'll be able to check and see, and have one behavior if something is zero.
And another behavior if it turns out not to be zero. There are some functions that we can look at with floats that are specific to floats, and that is basically regarding rounding. What do we do about rounding them to a certain number? Round, the function round takes a number that says how many significant digits do you want there to be? So, float, 1 will take my float number at the top, 3.14. And round it to one decimal place. Ceiling and floor are also a kind of rounding. Except that ceiling always rounds up, and floor always round down.
It doesn't follow the normal rules of rounding. Notice that ceiling is actually ceil, that that's the name of the function name, ceil And Floor is the other one. Let's try both of those out. Save that document, switch back to Firefox and reload, and you can see that it rounded 3.14 to be just 3.1. It rounded it up to 4 when I used Ceiling, and it rounded it down to 3 when I used Floor. Those are handy tools to have, especially when something doesn't divide evenly you can decide whether you want to round up or round down With the result.
Now with both integers and floating point numbers, we're able to ask PHP whether or not something is an integer or whether it is a float. We can do that like this. So for example, I can have PHP, we'll set integer equal to just 3. And then I'll just paste in a bit of code down here. And it says now, is integer. An integer is float an integer and then I'm going to use this method over here, which is, is underscore int to tell me true or false, is it an integer or not? We can do the same thing for float. Is it a float? And the method there is of course, is underscore float.
So it will return true false for each one of these is an integer or a float. And there's one more, which is we can use numeric on both of them, and we'll see what that gives us. Numeric is numeric. Alright, now notice that this is is_int, is_integer also works, that's an alias for it but is int is much more common, that's the one you'll see most people using. Let's try those out, we'll go over to firefox, and we'll reload the page. Is three an integer? Yes, it responded with true and true when it gets output as a string gets converted into a one. That's something we'll talk about when we talk about true and false later on. Is 3.14 an integer? No, it's false so it gets converted to nothing.
So that we see no result there. Is three afloat? No, it's not. Is 3.1 afloat? Yes, it is both of them though, are considered numeric. They both belong to this larger type, of being numeric. So this gives us the tools to be able to know whether something is afloat, whether it's an integer. Or even just when it's a number, regardless of which one of the two types it is. These kinds of tests can come in handy. Now that we understand how both integers and floating point numbers work in PHP let's take a look at another type, which is arrays.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97405 Viewers
61 Video lessons · 84635 Viewers
71 Video lessons · 68844 Viewers
56 Video lessons · 101302 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.