Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Not all numbers are as simple as integers. We often need to keep track of fractional values, like product that costs $1.99 or a distance like 2.5 miles. Those values are called floats, floating point values, real numbers or doubles: all names for the same thing. For decimal numbers we use the PHP variable type of Float.
Documentation
floats also known as " Floating point numbers", "doubles", or "real numbers", can be specified using any of the following syntaxes:
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
-
0:00
Not all numbers are as simple as integers.
-
0:02
We often need to keep track of fractional values like products that cost $1.99,
-
0:07
or distance like 2.5 miles.
-
0:10
Those values are called floats.
-
0:12
Floating point integers, real numbers or doubles, all names for the same thing.
-
0:18
For decimal numbers we use the PHP variable type float.
-
0:23
Let's start by commenting out these var dumps.
-
0:28
Now let's say we want to find the distance of a trip.
-
0:32
So we'll create two variables here, distance home and distance to work.
-
0:36
Distance_home, we'll set this equal to a floating point and number.
-
0:43
We simply set this variable equal to 1.2.
-
0:46
Enclose the statement with a semi colon.
-
0:50
Then we'll add our second floating point variable distance to work.
-
0:54
And will set this equal to 2.5.
-
0:58
Again ending with a semi-colon.
-
1:01
Now we can use the same type of operations we've done before.
-
1:06
Let's do a var dump, and we'll dump
-
1:10
the distance home plus the distance to work.
-
1:18
All right, now let's save and
-
1:21
run our script AS you can see we can take floating point numbers and
-
1:25
add them together and get returned a floating point value.
-
1:31
Now what if I were to add a non floating point number to this equation.
-
1:35
So let's say we'll and the variable num 3.
-
1:42
Number three is a whole number.
-
1:44
So let's save this script and run it again.
-
1:49
Now we see 6.7.
-
1:50
So if we take whole numbers and we sum them to floating point numbers
-
1:55
we're still going to get a floating point in return.
-
1:59
Let's add one more floating point number to make our results a whole number.
-
2:02
Let's add 0.3.
-
2:08
Again, run the script and
-
2:10
we see the results of seven, but the type is still a float.
-
2:16
Any time we're using a floating point value the result will be a float as well.
-
2:21
Most of the time,
-
2:22
you won't need to worry about defining a variable as an integer or a float.
-
2:26
PHP automatically handles the differences, so you don't have to.
You need to sign up for Treehouse in order to download course files.
Sign up