-7
<?php

function generateRandomString($length = 10, $alpha = false, $numeric = false) {

  if ($alpha)   { $characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ'; }
  if ($numeric) { $characters = '0123456789'; }

  $charactersLength = strlen($characters);
  $randomString = '';

  for ($i = 0; $i < $length; ++$i) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
  }

  if (substr($randomString, 0, 1) != 2) { // if doesn't start with 2
    generateRandomString($length, $alpha, $numeric); // try again
  }
  else {
    return $randomString;
  }
}

$num = generateRandomString(8, false, true);
echo ($num);

I need it to return a value starting with 2 but it doesn't return anything at all, no errors either. What am I doing wrong here?

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • 2
    `return generateRandomString(...);` – Jonnix Sep 05 '16 at 10:46
  • 3
    The question title says it call: recursive call does not return... – Álvaro González Sep 05 '16 at 10:46
  • 3
    1) There's nothing special about *recursively* calling a function. Substitute that inner `generateRandomString` call with a call to any other function like `substr` and the problem is obvious. 2) If your requirement is to generate a string that starts with `2`… uhm… how about you start with `$randomString = '2'` instead of producing potentially infinite recursion? – deceze Sep 05 '16 at 10:49
  • 2
    What's the point of having two different parameters `$alpha` and `$numeric` if they're mutually exclusive and they cannot be false (the default value) at the same time? – Álvaro González Sep 05 '16 at 10:51
  • 2
    Also… what if I call `generateRandomString(8, true, true)`? Then I *won't* receive an *alphanumeric* result. And if I call `generateRandomString(8, true)` the function will never finish. – deceze Sep 05 '16 at 10:51
  • 1
    I think in summary we can say this is just about the worst random string function in history. That's gotta be worth something. – deceze Sep 05 '16 at 10:53
  • 1
    Challenge Accepted. – Jonnix Sep 05 '16 at 10:53
  • @deceze Its just an example, I'll be testing for `!= 0` – eozzy Sep 05 '16 at 11:39
  • I don't mind the negative votes and sarcastic comments. I posted a question and learnt something new today, its all that matter. Yes it maybe the worst function but for someone using a recursive function for the first time, its probably not too bad. Also I hope some beginner stumbling upon the same issue will find this answer helpful. :) – eozzy Sep 05 '16 at 11:44

1 Answers1

3

Use return here, Change this

if (substr($randomString, 0, 1) != 2) { // if doesn't start with 2
  generateRandomString($length, $alpha, $numeric); // try again
}

to

if (substr($randomString, 0, 1) != 2) { // if doesn't start with 2
 return generateRandomString($length, $alpha, $numeric); // try again
}
Drone
  • 1,114
  • 1
  • 12
  • 31
  • 1
    Using a recursive function for the first time, good to learn that, thanks! The joke about recursion is so true — to understand recursion, you must first understand recursion. – eozzy Sep 05 '16 at 11:42