Join Kevin Skoglund for an in-depth discussion in this video Challenge: Convert to and from metric area, part of Easy PHP Projects: Measurement Conversion.
- View Offline
- In this movie, we're going to learn how to convert different measurement units to and from metric area. If you recall, area is equal to length times width. And, we express that area in square units. And, because it's a square unit, the length and the width are equal. That means that it could also be expressed as area equals length times length or length squared. Now, again, the area that we're measuring may not be a perfect square, but the unit of measure is what we're squaring here.
That's how we get square units. What that means is that the conversion units that we used for length are still useful when working with area. 1 foot equals 0.3048 meters. 1 square foot equals (0.3048 times 0.3048) square meters. Just the length squared. So, therefore, 1 square foot is equal to 0.09290304 square meters. Now, we could just simply take that number, figure out what the numbers are, do the multiplication, and plug those numbers into our script exactly like we did before using multiplication and division the same way we did when working with length to get the conversions.
However, I think it's actually easier if we keep using the existing units that we already have and, instead, just square those units. In other words, don't actually go find out what the value is. Let PHP figure it out for us. The way that we can do that is by using the pow function in PHP. This is named pow as short for power because you're raising the base number to the power of the exponent. The first argument that goes in is the base number. Second number is the exponent. In our case, the exponent will always be two because we're squaring each of the base numbers.
So, for example, if we wanted to raise 0.3048 to the second power, or square it, we would use pow in this way. Now, you have all the pieces that you need to do this on your own. You have the form already started and you now know how the conversions work. You know what the conversion formula that we're going to use is. As a challenge, try it for yourself. See if you can write convert_to_square_meters, convert_from_square_meters, and convert_area by modifying what we did when working with length.
The main things you're going to need to do are change the case values, that is, instead of looking for inches it's going to be square inches, and then you need to update the formulas that go with those using the pow function that we just talked about. After that, go one step further and add conversions for acres and hectares. I'll tell you that 1 acre equals 4046.8564224 square meters and 1 hectare equals 10,000 square meters. Try it on your own, I'll show you my solution in the next movie.
Along the way, series designer Kevin Skoglund introduces coding strategies, refactoring techniques, and useful skills including dynamically generating form select-option values and formatting decimal numbers and scientific notation.
- Converting length and area measurements
- Using functions to avoid repetition
- Refactoring to produce simpler code
- Converting volume and mass
- Generating form select-option values dynamically
- Formatting scientific notation
- Converting speed and temperature