title: Numbers to Roman Numbers with php
body: <p>I need to transform ordinary numbers to Roman numerals with php and I have this code:</p>
  
  <pre><code>        &lt;?php
  
  function roman2number($roman){
      $conv = array(
          array("letter" =&gt; 'I', "number" =&gt; 1),
          array("letter" =&gt; 'V', "number" =&gt; 5),
          array("letter" =&gt; 'X', "number" =&gt; 10),
          array("letter" =&gt; 'L', "number" =&gt; 50),
          array("letter" =&gt; 'C', "number" =&gt; 100),
          array("letter" =&gt; 'D', "number" =&gt; 500),
          array("letter" =&gt; 'M', "number" =&gt; 1000),
          array("letter" =&gt; 0, "number" =&gt; 0)
      );
      $arabic = 0;
      $state = 0;
      $sidx = 0;
      $len = strlen($roman);
  
      while ($len &gt;= 0) {
          $i = 0;
          $sidx = $len;
  
          while ($conv[$i]['number'] &gt; 0) {
              if (strtoupper(@$roman[$sidx]) == $conv[$i]['letter']) {
                  if ($state &gt; $conv[$i]['number']) {
                      $arabic -= $conv[$i]['number'];
                  } else {
                      $arabic += $conv[$i]['number'];
                      $state = $conv[$i]['number'];
                  }
              }
              $i++;
          }
  
          $len--;
      }
  
      return($arabic);
  }
  
  
  function number2roman($num,$isUpper=true) {
      $n = intval($num);
      $res = '';
  
      /*** roman_numerals array ***/
      $roman_numerals = array(
          'M' =&gt; 1000,
          'CM' =&gt; 900,
          'D' =&gt; 500,
          'CD' =&gt; 400,
          'C' =&gt; 100,
          'XC' =&gt; 90,
          'L' =&gt; 50,
          'XL' =&gt; 40,
          'X' =&gt; 10,
          'IX' =&gt; 9,
          'V' =&gt; 5,
          'IV' =&gt; 4,
          'I' =&gt; 1
      );
  
      foreach ($roman_numerals as $roman =&gt; $number)
      {
          /*** divide to get matches ***/
          $matches = intval($n / $number);
  
          /*** assign the roman char * $matches ***/
          $res .= str_repeat($roman, $matches);
  
          /*** substract from the number ***/
          $n = $n % $number;
      }
  
      /*** return the res ***/
      if($isUpper) return $res;
      else return strtolower($res);
  }
  
  /* TEST */
  echo $s=number2roman(6,true);
  echo "\n and bacK:\n";
  echo roman2number($s);
  
  
  ?&gt;
  </code></pre>
  
  <p>try this way but does not work:</p>
  
  <pre><code>echo $s=number2roman((.$row['id'].),true);
  echo "\n and bacK:\n";
  echo roman2number($s);
  </code></pre>
  
  <p>the problem is that I need to change numbers are readings of my sql database and do not know how to, from and through.</p>