- Impact
- 209
1 thing within PHP that some people have trouble with is recursive functions.
In case you are not sure what a recursive function is, a recursive function is a function that performs a task and may have to call itself to perform the same task again. Hopefully the following tutorial will enlighten you somewhat.
You maybe wondering why a recursive function might be necessary. The simple answer is that without recursive functions you are set to rely on nested if else statements which can get very messy. Also if you have nested if else statements you need to know how deep you need to go, a recursive function can effectively continuously go on (well until the server runs out of memory anyway)
You may have noticed that magic_quotes_gpc has received a lot of flack in the last couple of years. I am not going to go into why this is but what I will say is that magic_quotes_gpc attempts to make safe any input that is gained from form methods such as GET and POST it also does the same for COOKIE data (hence why it has gpc at the end of it's name).
The function we are going to create will check that this is enabled and make the arrays safe.
Firstly let me show you the whole code then I will explain it a bit at a time:-
There is nothing too technical within this code snippet but I will go through it regardless.
you will most likely know these 2 lines. The first line is simply the definition of the function. In this case the function is called clean_array. As you can see the function requires 1 variable and for the sake of the function it is referred to as $array. The second line simply starts the block of code. Line 3 I have decided to declare a variable as an array. The variable is called $temp and is only available within the function. This is so I can pass the cleaned data to this variable.
Now this is the meat of the function. We start of by ensuring that the input is indeed an array (we will deal with it later if it is not). After this we start a loop to break down the array. We need to keep the key and value for the array as of course you need to know what the data is. Please ignore the next if statement for now and we will come back to that. The else on the other hand contains another if statement that ensures magic_quotes_gpc is enabled (get_magic_quotes_gpc() retrieves the current setting for the option) and for this option 1 indicates that it is enabled. Within this if we simply use stripslashes to remove the slashes that magic_quotes_gpc added. We then input this value into the temp array giving it the same key as it had within the original array.
This is the final part of the function. Although it should not happen it is possible that someone may not use the function to check an array so we ensure we are covered in this instance. We do the same here as we did in the else statement earlier.
The final part is the ending of the function:-
We simply return the $temp array/variable to the calling script or function to do with it as it will.
Now lets as promised go back the recursive section of the function:-
Now what we are doing here is checking to see if the current value within the foreach loop is also an array (as you have probably guessed the built in is_array function as it's name suggests carries out this check). If the value is an array we call our own function assigning the output to the key of this array within the original array. As this function is calling another instance of this function this assignment will receive the output from return $temp; and assign it to the temp array. I know this can get a little confusing lets see it in action.
We can use this function by doing clean_array($array) but as we wish to see the output I will use print_r. The array I will use is:-
$array = array("age"=>"28","name"=>"O\'reilly?", "favorite_bands"=>array('Iron Maiden'=>array("members"=>"Paul Di\'Anno"), 'Dimmu Borgir', "Guns \'N\' Roses"));
As you can see there is a multidimensional array (an array within an array, in fact in this case there is an array within an array within an array). You would of course use this normally with $_POST, $_GET and $_COOKIE.
The output we receive from this is as follows:-
As you can see the indexes are 100% intact the only thing that has changed with these is that the slashes have miraculously disappeared.
If you think I have been unclear in this post please feel free to let me know and I will try and correct that.
Points to note.
I do realise that array_map could have been used to do this in a much simpler way however you may not get a full sense of how they work using this function and it may not always be possible.
Although recursive functions can be a very powerful tool you should have caution. As you will probably know an endless loop can cause problems on a server (and if it was not for your maximum execution time with the php configuration it would last forever, or at least until the server was restarted), so can a recursive function that is poorly implemented. If you create a condition in which a recursive function continuously calls itself and the condition to terminate is never met you will suffer a similar problem as an endless loop. Another problem you will have is your servers memory being consumed (or at least the max set by php) by 1 script. This can happen if you have a complex function that is poorly planned out.
**ANY CODE I WRITE AND PUBLISH ON NAMEPROS CAN BE CONSIDERED FREE TO USE. IF HOWEVER YOU WISH TO DISTRIBUTE THE CODE EITHER BY ITSELF OR WITHIN AN APPLICATION CREDIT MUST BE GIVEN**
In case you are not sure what a recursive function is, a recursive function is a function that performs a task and may have to call itself to perform the same task again. Hopefully the following tutorial will enlighten you somewhat.
You maybe wondering why a recursive function might be necessary. The simple answer is that without recursive functions you are set to rely on nested if else statements which can get very messy. Also if you have nested if else statements you need to know how deep you need to go, a recursive function can effectively continuously go on (well until the server runs out of memory anyway)
You may have noticed that magic_quotes_gpc has received a lot of flack in the last couple of years. I am not going to go into why this is but what I will say is that magic_quotes_gpc attempts to make safe any input that is gained from form methods such as GET and POST it also does the same for COOKIE data (hence why it has gpc at the end of it's name).
The function we are going to create will check that this is enabled and make the arrays safe.
Firstly let me show you the whole code then I will explain it a bit at a time:-
PHP:
function clean_array($array)
{
$temp = array();
if (is_array($array))
{
foreach ($array as $key=>$value)
{
if (is_array($value))
{
$temp[$key] = clean_array($value);
}
else
{
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
$temp[$key] = $value;
}
}
}
else
{
if (ini_get('magic_quotes_gpc') == 1)
{
$array = stripslashes($array);
}
$temp = $array;
}
return $temp;
}
There is nothing too technical within this code snippet but I will go through it regardless.
PHP:
function clean_array($array)
{
$temp = array();
you will most likely know these 2 lines. The first line is simply the definition of the function. In this case the function is called clean_array. As you can see the function requires 1 variable and for the sake of the function it is referred to as $array. The second line simply starts the block of code. Line 3 I have decided to declare a variable as an array. The variable is called $temp and is only available within the function. This is so I can pass the cleaned data to this variable.
PHP:
if (is_array($array))
{
foreach ($array as $key=>$value)
{
if (is_array($value))
{
$temp[$key] = clean_array($value);
}
else
{
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
$temp[$key] = $value;
}
}
}
Now this is the meat of the function. We start of by ensuring that the input is indeed an array (we will deal with it later if it is not). After this we start a loop to break down the array. We need to keep the key and value for the array as of course you need to know what the data is. Please ignore the next if statement for now and we will come back to that. The else on the other hand contains another if statement that ensures magic_quotes_gpc is enabled (get_magic_quotes_gpc() retrieves the current setting for the option) and for this option 1 indicates that it is enabled. Within this if we simply use stripslashes to remove the slashes that magic_quotes_gpc added. We then input this value into the temp array giving it the same key as it had within the original array.
PHP:
else
{
if (get_magic_quotes_gpc())
{
$array = stripslashes($array);
}
$temp = $array;
}
This is the final part of the function. Although it should not happen it is possible that someone may not use the function to check an array so we ensure we are covered in this instance. We do the same here as we did in the else statement earlier.
The final part is the ending of the function:-
PHP:
return $temp;
}
We simply return the $temp array/variable to the calling script or function to do with it as it will.
Now lets as promised go back the recursive section of the function:-
PHP:
if (is_array($value))
{
$temp[$key] = clean_array($value);
}
Now what we are doing here is checking to see if the current value within the foreach loop is also an array (as you have probably guessed the built in is_array function as it's name suggests carries out this check). If the value is an array we call our own function assigning the output to the key of this array within the original array. As this function is calling another instance of this function this assignment will receive the output from return $temp; and assign it to the temp array. I know this can get a little confusing lets see it in action.
We can use this function by doing clean_array($array) but as we wish to see the output I will use print_r. The array I will use is:-
$array = array("age"=>"28","name"=>"O\'reilly?", "favorite_bands"=>array('Iron Maiden'=>array("members"=>"Paul Di\'Anno"), 'Dimmu Borgir', "Guns \'N\' Roses"));
As you can see there is a multidimensional array (an array within an array, in fact in this case there is an array within an array within an array). You would of course use this normally with $_POST, $_GET and $_COOKIE.
The output we receive from this is as follows:-
PHP:
Array
(
[age] => 28
[name] => O'reilly?
[favorite_bands] => Array
(
[Iron Maiden] => Array
(
[members] => Paul Di'Anno
)
[0] => Dimmu Borgir
[1] => Guns 'N' Roses
)
)
As you can see the indexes are 100% intact the only thing that has changed with these is that the slashes have miraculously disappeared.
If you think I have been unclear in this post please feel free to let me know and I will try and correct that.
Points to note.
I do realise that array_map could have been used to do this in a much simpler way however you may not get a full sense of how they work using this function and it may not always be possible.
Although recursive functions can be a very powerful tool you should have caution. As you will probably know an endless loop can cause problems on a server (and if it was not for your maximum execution time with the php configuration it would last forever, or at least until the server was restarted), so can a recursive function that is poorly implemented. If you create a condition in which a recursive function continuously calls itself and the condition to terminate is never met you will suffer a similar problem as an endless loop. Another problem you will have is your servers memory being consumed (or at least the max set by php) by 1 script. This can happen if you have a complex function that is poorly planned out.
**ANY CODE I WRITE AND PUBLISH ON NAMEPROS CAN BE CONSIDERED FREE TO USE. IF HOWEVER YOU WISH TO DISTRIBUTE THE CODE EITHER BY ITSELF OR WITHIN AN APPLICATION CREDIT MUST BE GIVEN**
Last edited:






