How to use return inside a recursive functions in php

徘徊边缘 提交于 2019-12-31 03:19:07

问题


Here is my question I am trying to create a random bar code for my application, I want to check if that code is already in the column than generate a new number, check it again if its unique return it to the caller else generate again. I am using a recursive function for this purpose. I have added number 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 its sometime showing me "Not Set", but I want it to always generate a unique number. I am not inserting the data so its not a problem that all 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. Thanks


回答1:


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.)




回答2:


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();



回答3:


why don't you do 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.

Does it have to be just numbers? if so just use the pkey and add like 10000 on to it for looks. or such.

UPDATE After careful analysis - there is nothing wrong with it but:

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

See you return the unique value 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. Therefor it is always set...

NM: I'm tired its 4:30 been up about 17hrs, lol




回答4:


What you are trying to do is:

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.



来源:https://stackoverflow.com/questions/31624313/how-to-use-return-inside-a-recursive-functions-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!