title: PHP If true rerun the function until condition is false
body: <p>So I have a function that creates random string of characters, something like this:</p>
  
  <pre><code> function createToken($len = 5) {
          $set = '123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
          $setLen = strlen($set);
          $token = '';
          for ($i = 0; $i &lt; $len; $i++) {
              $token .= $set[rand(0, $setLen - 1)];
          }
          return $token;
      }
  </code></pre>
  
  <p>Now I need to run this function and check if the return token is already in the database. Since I'm using Laravel I can check that with:</p>
  
  <pre><code>$modelName-&gt;whereToken($token)-&gt;exists()
  </code></pre>
  
  <p>And it will return true or false, now if it returns true I need to rerun the function and do the generation again then do the if exists again and do that over and over until it finds one that returns false on ->exists().</p>