Why this function is entering an infinite loop?

-3

I have done this function to return a unique code (it checks if it already exists searching in a webservice), but when I run it, it runs forever, it does not return a result.

public function generateuniquecode(){
    function generatecode(){
      $upper = implode('', range('A', 'Z')); // ABCDEFGHIJKLMNOPQRSTUVWXYZ
      $nums = implode('', range(0, 9)); // 0123456789
      $alphaNumeric = $upper.$nums; // ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
      $code = '';
      for($i = 0; $i < 6; $i++) {
          $code .= $alphaNumeric[rand(0, strlen($alphaNumeric) - 1)];
      }
      return $code; // ex: X0XX0X
    }
    $exists = false;
    while($exists == false) {
      $code = generatecode();
      $response = $this->seachcardbycode($code);
      if($response['success'] == false){
        if($response['data']['code'] == 1){
          $exists = false;
        }else{
          die('code 0');
          // code 0
          // curl call error
        }
      }else{
        $exists = true;
      }
    }
    return $code; 
  }

The seachcardbycode function returns an array like this:

array(
      "success"=>false / true,
      "data"=>outro array
    );
    
asked by anonymous 09.05.2018 / 19:51

1 answer

0

Apparently the problem is in your code, in the section where the IF starts.

if($response['success'] == false)

To be more precise in the response would be interesting you post the code of the entire function seachcardbycode . To see the possibilities of $response['success'] being changed. At first this is always false so the else will never be fired, and the value of $existes will never be changed.

If you do not pass% to% with no object where success is equal to false , this code will actually stay in an infinite loop.     

09.05.2018 / 20:01