**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

Let's take what we've learned about variables and operators to create a simple unit converter that can convert pounds to kilograms and miles to kilometers.

#### Conversions

```
$lb_to_kg = 0.453592;
$mile_to_km = 1.60934;
```

1 pound = 0.453592 kilogram

1 kilogram = 2.20462 pounds

1 mile = 1.60934 kilometers

1 kilometer = 0.621371 miles

Let's take what we've learned about
variables and operators to create a simple
0:00

unit converter that can convert pounds
to kilograms and miles to kilometers.
0:04

You can reverse these calculations,
0:09

as well, to determine the number of miles
in a kilometer, and pounds in a kilogram.
0:10

Check the teacher's notes for
0:15

more of the equations used to
calculate these conversions.
0:17

Do you remember the two
basic parts of writing code?
0:20

The storage and retrieval of data and
the logic that tells what and
0:23

when to do something with that data.
0:27

So first we'll need to store two pieces
of data, the floating point value for
0:30

the conversion and
the number we want to convert.
0:34

Then we'll retrieve that data and
perform the calculations.
0:38

Then we'll finally display the results.
0:41

So let's get started.
0:44

Let's create a new file named units.php.
0:46

We start by adding the opening and
closing php tags and
0:53

then we're ready to start
writing our program.
0:56

Comments are a great way to start.
1:00

So let's outline what we'll
be doing using comments.
1:02

Let's start with pounds to kilograms.
1:05

The first thing we need
is number in pounds
1:08

that we want to convert to kilograms.
1:14

Then we need the floating point value for
1:21

the pound to kilogram conversion.
1:27

Then we'll use the variables
1:34

above to calculate pounds multiplied
1:40

by the kilogram conversion.
1:46

And finally,
we display the pounds to kilograms.
1:53

Then we'll also convert
miles to kilometers.
2:00

Again, we start with
the number in miles we want to
2:04

convert to kilometers.
2:10

Then we need the floating point value, For
2:17

the mile to kilometer conversion.
2:22

Then we need to use the variables
2:29

above to calculate miles multiplied
2:34

by the kilometer conversion.
2:41

And finally,
we'll display the miles to kilometers.
2:46

Now we're ready to start
creating our variables.
2:53

We'll start with our
variable name $pounds.
2:58

You can assign this
whatever value you want.
3:01

I'm going to use 140.
3:04

We'll then create our next
variable named $lbs_to_kg.
3:07

And we'll set this equal to 0.453592,
3:14

which is how many kilograms
go into one pound.
3:18

We can now perform the calculation and
3:24

assign it to a third
variable named $kilograms.
3:26

$pounds * $lb_to_kg
3:32

will give us our kilograms.
3:37

And finally,
we can display the results to the screen.
3:43

Let's start this with some text.
3:49

Our weight, Echo our $pounds.
3:51

Then we'll echo some more text.
4:00

Our lb = $kilograms.
4:03

Echo "kg".
4:10

Now, let's run the script and
see what we get.
4:15

Great, just what we want.
4:19

The weight, 140 pounds,
4:22

equals 63.50288 kilograms.
4:25

Let's add a space right here.
4:30

And now we're ready to set
up our miles to kilometers.
4:33

We start with a variable named $miles and
we'll set this equal to 2.5.
4:39

We then create a variable
named $mile_to_km and
4:46

we'll give it a value of 1.60934,
4:51

which is how many
kilometers are in one mile.
4:54

Now we perform the calculation and assign
it to the third variable, $kilometers.
5:01

$miles * $mile_to_km will
give us our kilometers.
5:09

And finally,
we display the results to the screen.
5:18

Echo our text will say "Distance:".
5:23

Echo our $miles and
5:28

then "miles =",
5:33

our $kilometer.
5:37

And then we'll echo out km.
5:44

Now let's run the script and
see what we get.
5:50

Great, we can see distance
added to the end.
5:54

2.5 miles equals 4.02335 kilometers.
5:58

We'll learn more about
formatting in the next section.
6:04

Fantastic job putting your new knowledge
of variables and operators to work.
6:08

Can you reverse the operations and
convert kilograms to pounds, and
6:13

kilometers to miles?
6:16

Or choose a different conversion to
implement, such as Fahrenheit to Celsius?
6:18

As you practice,
6:23

you'll become much more comfortable with
using your PHP skills to solve problems.
6:23

In the next section,
we'll explore different data types for
6:29

variables, use conditional operators, and
6:32

start implementing conditional logic
to create a daily exercise program.
6:35

So hurry back to continue
flexing our muscles.
6:40

You need to sign up for Treehouse in order to download course files.

Sign up