title: Base10 to base64 url shortening
body: <p>I'm coding an url shortener function for a project in which I'm learning php, here is the code (btw I suppose that <code>global</code> here is not a good thing to do :P):</p>
  
  <pre><code>$alphabet = array(1 =&gt; "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                  "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                  "0","1","2","3","4","5","6","7","8","9","_","-");
  
  function shorten($id){
      global $alphabet;
      $shortenedId = "";
      while($id&gt;0){
          $remainder = $id % 64;
          $id = $id / 64;     
          $shortenedId = $alphabet[$remainder].$shortenedId;
      }
      return $shortenedId;
  }
  </code></pre>
  
  <p>The code is taken from <a href="http://en.wikipedia.org/wiki/Base_36#Python_Conversion_Code" rel="noreferrer">this Wikipedia article</a>  and adapted to php. My problem is that when I pass a multiple of 64 to the function I get a wrong (for my purpose) result, for instance 128 returns b which is not correct, it should have been aaa, but that's too long for a 3-digit number.</p>
  
  <p>Also I'm starting to think that there's something wrong in this code, if I pass 1'000'000'000'000 as <code>$id</code> I get nItOq... I feel it's wrong because a url shortening service like bit.ly returns a 6 number id if I use it, and I don't think that this algorithm is better than theirs.</p>
  
  <p>So, two questions:</p>
  
  <ul>
  <li>do you spot any bug in the above code?</li>
  <li>how to manage 64-multiple ids? Do I have to just ignore them and pass to the next one?</li>
  </ul>