What does PHP stand for?

00277With a name like PHP: Hypertext Preprocessor” you can be sure that PHP will have good support for recursion.

A recursive function is simply a function that calls itself. You don’t need to do anything special in the function’s definition to define it as recursive. You simply call the function from within its own definition. Recursion is an alternative to iteration, which means that it can be used in place of a looping structure to achieve the same result. In simple situations, the use of a loop or recursion is simply a matter of preference. However, some problems are better solved by recursion. Let’s try a simple example that shows how recursion is a great solution for traversing nested arrays.

Let’s use an integration project to illustrate the power of recursion. In this example we are building an integration to the personality testing API of Schaefer Testing Company. This fictional company does personality testing to help hiring managers filter out candidates who won’t be a good fit. In our interface we want to display a single result along with a link to view the complete results. As it happens, Schaefer Testing provides the results in a large array with each element containing the percentage score. To make matters more complex, some scores are arrays of more detailed scores and some scores may be non-numeric. We will discard non-numeric scores since they do not represent a percentage score and will not average well.

To make the example simpler I have removed all of the code for creating the connection and we begin with the array of scores.

<?php

$scores = array(
o'reading'=>91,
o'attitude'=>95,
o'math'=>68,
o'advanced'=>array(
o'problem solving'=>array(
o'simple'=>89,
o'complex'=>77,
o'lateral'=>50,
o),
o'comprehension'=>90,
o'experience'=>53,
o),
o'typing'=>array(
o'accuracy'=>75,
o'speed percentile'=>50,
o'speed'=>'40wpm',
o),
o'engagement'=>81,
);
$total = get_score_total($scores);
$count = get_score_count($scores);
if($count == 0){$average = '-';}else{
o$average = round($total/$count);
}

echo 'Total: '.$total;
echo '<br>';
echo 'Count: '.$count;
echo '<br>';
echo 'Average: '.$average;


function get_score_total($scores_array){
o$score_collector = 0;
oforeach($scores_array as $score){
oif(is_array($score)){
o$score_collector += get_score_total($score);
o}elseif(is_numeric($score)){
o$score_collector += $score;
o}
o}
oreturn $score_collector;
}

function get_score_count($scores_array){
o$count_collector = 0;
oforeach($scores_array as $score){
oif(is_array($score)){
o$count_collector += get_score_count($score);
o}elseif(is_numeric($score)){
o++$count_collector;
o}
o}
oreturn $count_collector;
}

 

To get the average we use one recursive function to get the total score, and another similar function get the number of scores in the array. On line 36 get_score_total() is defined, taking an array of scores as a parameter.

First we set the score collector to zero. This variable will be set within the scope of each individual function so it can be different in every iteration. Even if we are in an instance of the function called in recursion, the zero is appropriate because we will always be counting the score total for the current array and any nested arrays.

The meat of the function is the foreach loop that will iterate over every score in the array. First, the if statement on line 39 will check if this score is an array of scores. Then the magic of recursion happens on line 40. We pass in the array of scores and add the result to the score collector. Function execution is paused in the outer function until the inner function is complete.

If the score is not an array we check to see if it is numeric and then add it to the score collector.

Finally, when the loop is complete we will return the collector so it can be added to any parent iterations of this function, or finally return to the scope in which the function was called.

One thought on “What does PHP stand for?

Leave a Reply

Your email address will not be published. Required fields are marked *