Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

PHP Unit converter

Here's my unit converter based on the extra credit on the new PHP Basics course.

I didn't know how to set up a php console like in Workspaces so I put it on a web page. I need to improve the responsive web design but I'll do that later. http://www.jonniegrieve.co.uk/jg-lab.co.uk/phpbasics-2016/unit.php

    <?php

    /*Store Values*/

    //pounds,kg, miles, kilos
    $pounds = 140;
    $kilograms = 140;
    $miles = 2.5;
    $kilometers = 2.5;
    $fahrenheit = 10;
    $celsius = 10;


    /*Conversion values*/

    //floating point value for the pound to kilogram conversion
    $lb_to_kg = 0.453592;
    //reverse calculation;
    $kg_to_lb = 2.20462;
    //floating point value for miles to kilometor calculation.
    $mile_to_km  = 4.02336;
    //reverse calculation
    $km_to_mile = 1.55343;
    //integer value for fahrenheight to celsius
    $fh_to_cs = 10;
    //reverse calculation
    $cs_to_fh = 50;

    //Make the conversion calculations
    $convertKilograms = $pounds * $lb_to_kg;
    $convertPounds = $kilograms * $kg_to_lb;

    $convertKilometers = $miles * $mile_to_km;
    $convertMiles = $kilometers * $km_to_mile;

    $convertFahrenheit = $celsius * $cs_to_fh;
    $convertCelsius = $fahrenheit * $fh_to_cs; 


    /*display the pounds to kilograms
    echo "<p>Weight: " . $pounds . " lb = " . $convertKilograms . "kg</p>";
    echo "Weight: " . $kilograms . " kg = " . $convertPounds . "lb\n\n";

    echo "Distance: ";
    echo $miles . " miles = " . $kilometers . " km ";
    echo $kilometers . "kilometers = " . $miles . " miles ";*/


    ?>

    <header>
        <h1>PHP: Simple Unit Converter</h1>

        <p>Assumes the following values.... </p>
    <div id = "unitsContainer"> 
        <div class = "units">   

            <ul>
                <li>Pounds (140lb)</li>
                <li>Kilograms (140kg)</li>
                <li>Miles (2.5)</li>
                <li>Kilometers(2.5)</li>
                <li>Fahrenheit (10f);</li>
                <li>Celsius = (10&deg;C);</li>
            </ul>
        </div>



        <div class = "units">
            <ul>
                <li>Pounds (lb) to Kilograms (kg) 0.453592;</li>
                <li>Kilograms (kg) to Pounds (lb) 2.20462; </li>
                <li>Miles (m) to Kilometers (km) = 4.02336</li>
                <li>Kilometers (km) to Miles (m) = 1.55343 </li>
                <li>Fahrenheit (f) to Celsius (&deg;) = 10</li>
                <li>Celsius (&deg;) to Fahrenheit (f) = 50</li>
            </ul>
        </div>
    </div>
    </header>

    <div id = "content">
        <div class="box">

            <h1>Pounds to KG: </h1>
            <p>Calculation: <?php echo $convertKilograms . "kgs"; ?></p>
            <p>Reverse:  <?php echo $convertPounds . "lbs"; ?></p>

        </div>

        <div class="box">

            <h1>Fahrenheit to Celsius:</h1>
            <p>Calculation:  <?php echo $convertCelsius; ?></p>
            <p>Reverse:      <?php echo $convertFahrenheit; ?></p>

        </div>

        <div class="box">

            <h1>Miles to Kilometers:</h1>
            <p>Calculation:  <?php echo $convertKilometers; ?></p>
            <p>Reverse:      <?php echo $convertMiles; ?></p>

        </div>

    </div>
    <?php 


    ?>

    <footer>
    <p>Conversion Measurements sourced by: <a href="http://www.worldwidemetric.com/measurements.html">worldwidemetric.com</a></p> 
    </footer>
</body>

</html>