Converting units of volume I am creating a volume converter. It allows users to enter a value and select the unit to convert from and the one it will be converted into. It uses a &lt;select&gt; tag for user input selection.

The &lt;select&gt; looks like this:

&lt;select name="from"&gt;
  &lt;option value=0&gt;--select--&lt;/option&gt;
  &lt;option value=1&gt;cm3&lt;/option&gt;
  &lt;option value=2&gt;m3&lt;/option&gt;
  &lt;option value=3&gt;Feet3&lt;/option&gt;
  &lt;option value=4&gt;Gallons(UK)&lt;/option&gt;
  &lt;option value=5&gt;Gallons(US)&lt;/option&gt;
  &lt;option value=6&gt;Inches3&lt;/option&gt;
  &lt;option value=7&gt;Litres&lt;/option&gt;
  &lt;option value=8&gt;Yards3&lt;/option&gt;
  &lt;option value=9&gt;Quart(UK)&lt;/option&gt;
&lt;/select&gt;


And there is another one similar to this with the name "to".

After that I did the following in PHP:

$from=$_POST['from'];
$to=$_POST['to'];

function assign($from,$val)
      {
          switch ($from)
          {
              case 1:
                  $fromu="cm&lt;sup&gt;3&lt;/sup&gt;";
                  $cm=1;
                  $me=0.000001;
                  $ft=0.000035315;
                  $gk=0.000219969;
                  $gs=0.000264172;
                  $in=0.061024;
                  $li=0.001;
                  $ya=0.000001308;
                  $qtk=0.000879877;
                  break;
               case 2:
                    $fromu="m&lt;sup&gt;3&lt;/sup&gt;";
                    $cm=1000000;
                    $me=1;
                    $ft=35.32;
                    $gk=220;
                    $gs=264;
                    $in=61024;
                    $li=1000;
                    $ya=1.308;
                    $qtk=879.877;
                    break;


There are 7 more cases based on the html select "from" and each holds a value of conversion for each unit relative to the selection made.

It ends with:

        echo "&lt;br&gt;&lt;br&gt;&lt;table align=center&gt;
     &lt;tr&gt;&lt;td&gt;&lt;h3&gt; ",$val," ",$fromu," equivalent is &lt;/h3&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$cm),"&lt;/u&gt; CentiMeter&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$me),"&lt;/u&gt; Meter&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$ft),"&lt;/u&gt; Feet&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$gk),"&lt;/u&gt; Gallons(UK)&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$gs),"&lt;/u&gt; Gallons(US)&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$in),"&lt;/u&gt; Inches&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$li),"&lt;/u&gt; Litres&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$ya),"&lt;/u&gt; Yards&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr align=right&gt;&lt;td&gt;&lt;u&gt; ",(double)($val*$qtk),"&lt;/u&gt; Quart(UK)&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";
   }
   assign($from,$val);
}


How do I optimize this in a way that I can save all the conversion values (I'm thinking possibly in an associative array) and still be able to display one conversion based on user selection in the "to" select instead of outputting all of the converted units as seen above?
Convert the from to a single unit, and convert that single unit to to. You don't have to compute conversions for every single pair.