How to use return inside a recursive function in PHP

Posted on

Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should. If you have an existing PHP-based website or application that is experiencing¬†performance issues, let’s get thinking about How to use return inside a recursive function in PHP.

Here is my question: I am trying to create a random bar code for my application. I want to check that if that code is already in the column, then generate a new number. Check it again. If it’s unique, return it to the caller, else generate again.

I am using a recursive function for this purpose. I have added numbers 1,2,3,4 inside my database so every time it runs. It has to show me 5,6,7,8,9 or 10.

Here is my function:

function generate_barcode(){
    $barcode = rand(1,10);
    $bquery = mysql_num_rows(mysql_query("SELECT * FROM stock_item WHERE barcode='$barcode'"));
    if($bquery==1){
        generate_barcode();
    }else{
        return $barcode;    
    }
 }

And I just tested it like this:

 $a = generate_barcode();
 if(isset($a))
 {
   echo $a;
 }
 else
 {
  echo 'Not Set';
 }  

So the problem is that it is sometimes showing me “Not Set”, but I want it to always generate a unique number. I am not inserting the data, so it’s not a problem that all of the numbers are reserved.

Someone just guide me and let me know what is wrong with the code. I can use other approaches to do that, but I need to know what is wrong with the supplied code.

Solution :

You need to return the generated number from your recursive call too, like:

function generate_barcode() {
  $barcode = rand(1, 10);
  $bquery = mysql_num_rows(mysql_query("SELECT * FROM stock_item WHERE barcode='$barcode'"));
  if ($bquery == 1) {
    return generate_barcode(); // changed!
  }
  else {
    return $barcode;
  }
}

(You should include some kind of exit for the case that all numbers are ‘taken’. This current version will call itself recursively until the PHP recursion limit is reached and will then throw an error.)

A return statement passes a value back to the immediate caller of the current function’s call-frame. In the case of recursion, this immediate caller can be another invocation of that same function.

You can counter this by doing the following:

Change:

generate_barcode();

to:

return generate_barcode();

Do it like this:

$hash = md5( microtime().rand(0, 1000) );

Adding a time component means it will pretty much be unique. Unless you have like 32^32 of them.

If it has to be just numbers, just use the pkey and add like 10000 on to it for looks. or such.

After careful analysis – there is nothing wrong with it, but:

$a = generate_barcode();
if(isset($a))  <<< this bit

See you return the unique value and then you say it’s isset my unique value, and $a will always be set, because if it’s not you recurse the function until it is, and then you return it. Therefore it is always set…

You are trying to do:

while(true) {
    $barcode = rand(1,10);
    $bquery = mysql_num_rows(mysql_query("SELECT * FROM stock_item WHERE barcode='$barcode'"));
    if($bquery===0){
        break;
    }
}

echo $barcode;

However, this will obviously only work for 10 bar codes and leading to an endless loop after that – meaning it is not the right approach to create a large number of bar codes.

Instead I would suggest to use an auto_increment to generate the bar code.

Btw, the mysql extension is deprecated. Use mysqli or PDO for new code.

Leave a Reply

Your email address will not be published. Required fields are marked *