Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:58:56+00:00 2026-05-26T03:58:56+00:00

I am trying to better understand how a function works. I am not familiar

  • 0

I am trying to better understand how a function works. I am not familiar with how the function below works and I was looking for some help and possibly an explanation. I get most of it except one part that uses math functions like floor(). The part that confuses me the most is the do…while loop. How exactly does using $id, floor() and $base convert a long url into a short url? Also, what exactly is it iterating through where you need a do…while loop to do it?

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$base  = strlen($chars);     // base 62
$site  = 'http://short.co';  // Replace with your domain

// ...Connect to MySQL server here...

function shorten_url($url)
{
  global $chars, $site, $base;

  $hash  = md5($url); 
  $alnum = NULL; //

  // Check if URL is already exist on db
  // Otherwise add this URL to table
  $res = mysql_query("SELECT id FROM urls WHERE url_hash='$hash'");

  if (mysql_num_rows($res)) {
    $row = mysql_fetch_object($res);
    $id = $row->id;
  } else {
    mysql_query("INSERT INTO urls (url, url_hash) VALUES ('$url', '$hash')");
    $id = mysql_insert_id();
  }

  // Convert id to base 62 and decode to alphanumeric
  do {
    $alnum = $chars[($id%$base)].$alnum;
  } while ($id = floor($id/$base));

  return "$site/$alnum";
}

If someone could provide an explanation without a ton of math jargon…I would appreciate it. Thanks.

I got the function from: http://bsd-noobz.com/blog/how-to-create-url-shortening-service-using-simple-php

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:58:56+00:00Added an answer on May 26, 2026 at 3:58 am

    You are probably only used to thinking of numbers that have 0 through 9 in them. What would have happened if we had 12 fingers instead of 10? Maybe we would have made another digit beyond 9?

    In your code, there are 62 different “digits” in your number system. You start counting with “a”, then “b”, then “c”, and so forth. When you run out of them, you start using two digits – “aa”, then “ab” until you run out those… then add another digit.

    When you create a new shortened URL, you insert it in a database and it gets an “id” number. The function converts it from the 10 digit numbers (called “base 10”) to the 64 digit numbers.

    When you want to access a URL, you convert back to base 10, look it up in the database, and serve it back to the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to better understand when I should and should not use Iterators.
I've seen some examples, but trying to better understand how Resource files work, such
I am trying to understand Http/Https a little better and possibly what headers I'm
I'm trying to understand better how data binding works in .net. I was checking
I am trying to better understand basic concepts in OOP. What are static and
I'm trying to better understand the concept of 'autocommit' when working with a Postgres
I'm trying to better understand what the stack trace means. I have a two
I'm trying to understand JQ better. I'm calling an JQ object $(.FamiliesList li li
Trying to understand Ruby a bit better, I ran into this code surfing the
I'm trying to understand the django admin better and at the same time, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.