递归函数说明

I have a question about a recursive function. I have a good idea about what it's doing but I need it explained to me in a bit more detail. I understand that it's passing $val, the $pattern to check for and boolean of $suspect. But why call the function recursively back to itself? I suppose what I think it's trying to do is check if $val is an array, if it is an array then loop through it and take the $item and pass that item (Ex: [$_POST['name']..) and see if it's an array. Obviously, it would be a string and at that point the first if statement would fail because the $item is a string and then it would jump down to the the preg_match function checking to see if that string contains the pattern we set to check for in $pattern.

Am I on the right track? My question is why pass the function recursively back to itself?

CODE:

// Assume nothing is suspect
$suspect = false;

// Create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';

// Function to check for suspect phrases

function isSuspect($val, $pattern, &$suspect) {
if(is_array($val)) {
    foreach($val as $item) {
        isSuspect($item, $pattern, $suspect);
    }
} else {
    // If one of the suspect phrases is found, set Boolean to true
    if (preg_match($pattern, $val)) {
        $suspect = true;
        }
    }
}

   // check the $_POST array and any subarrays for suspect content
   isSuspect($_POST, $pattern, $suspect);

Consider passing a value like this into the isSubject function:

$val = array(arrray(array(1,2,3),4),5);

Through recursion you make shure that:

  1. every time you hit an array, dig deeper
  2. every time you hit a string, compare it to the pattern
  3. keep track if we found the subject with a global state variable called $suspect

so to say in the given example, the

  1. level of recursion: examine array(arrray(array(1,2,3),4),5);
  2. level of recursion: examine arrray(array(1,2,3),4) and 5
  3. level of recursion: examine array(1,2,3) and 4
  4. level of recursion: examine 1 and 2 and 3