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

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

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

0:09
You can reverse these calculations,

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

0:15
Check the teacher's notes for

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

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

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

0:27
when to do something with that data.

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

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

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

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

0:44
So let's get started.

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

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

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

1:00
Comments are a great way to start.

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

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

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

1:14
that we want to convert to kilograms.

1:21
Then we need the floating point value for

1:27
the pound to kilogram conversion.

1:34
Then we'll use the variables

1:40
above to calculate pounds multiplied

1:46
by the kilogram conversion.

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

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

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

2:10
convert to kilometers.

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

2:22
the mile to kilometer conversion.

2:29
Then we need to use the variables

2:34
above to calculate miles multiplied

2:41
by the kilometer conversion.

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

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

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

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

3:04
I'm going to use 140.

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

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

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

3:24
We can now perform the calculation and

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

3:32
$pounds * $lb_to_kg

3:37
will give us our kilograms.

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

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

3:51
Our weight, Echo our $pounds.

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

4:03
Our lb = $kilograms.

4:10
Echo "kg".

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

4:19
Great, just what we want.

4:22
The weight, 140 pounds,

4:25
equals 63.50288 kilograms.

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

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

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

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

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

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

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

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

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

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

5:28
Echo our $miles and

5:33
then "miles =",

5:37
our $kilometer.

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

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

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

5:58
2.5 miles equals 4.02335 kilometers.

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

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

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

6:16
kilometers to miles?

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

6:23
As you practice,

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

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

6:32
variables, use conditional operators, and

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

6:40
So hurry back to continue flexing our muscles.
You need to sign up for Treehouse in order to download course files.
Sign up