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