Length Converter
Results 1 to 4 of 4

Thread: Length Converter

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2

    Exclamation Length Converter

    Hey there I am trying to create a simple converter for length like cm and m etc, to put onto my simple site that I have started to build, I just don't know how to start it off or what making sure that the code doesnt look like I have copied and pasted it as its for an assignment in uni. I hope someone can help me!!

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,915
    I would create a set of static methods in a class, naming everything so that it's obvious what each method does, e.g.:
    PHP Code:
    <?php

    class LengthCoverter
    {
       const 
    CM_PER_INCH 2.54;
       public static function 
    cmToInches($length)
       {
          return 
    $length self::CM_PER_INCH;
       }
       public static function 
    inchesToCm($length)
       {
          return 
    $length self::CM_PER_INCH;
       }
    }

    // Sample usage:
    echo "12 inches = ".LengthCoverter::inchesToCm(12)." cm.<br />\n";
    echo 
    "7.62 cm = ".LengthCoverter::cmToInches(7.62)." inches.";
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2

    Exclamation Putting it into a form

    Quote Originally Posted by NogDog View Post
    I would create a set of static methods in a class, naming everything so that it's obvious what each method does, e.g.:
    PHP Code:
    <?php

    class LengthCoverter
    {
       const 
    CM_PER_INCH 2.54;
       public static function 
    cmToInches($length)
       {
          return 
    $length self::CM_PER_INCH;
       }
       public static function 
    inchesToCm($length)
       {
          return 
    $length self::CM_PER_INCH;
       }
    }

    // Sample usage:
    echo "12 inches = ".LengthCoverter::inchesToCm(12)." cm.<br />\n";
    echo 
    "7.62 cm = ".LengthCoverter::cmToInches(7.62)." inches.";

    Ok thanks for that, quick question if I was wanting to do it as I wanted to ask the user what value they wanted converted in a form how would I go about doing that?

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    See the manual page variables.external to learn how to get data from external sources.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •