PHP Recursive Function and Variable-Length Argument Lists
PHP Functions with Security Considerations - Part 3
Foreword: In this part of the series I talk about PHP Recursive Function and Variable-length Argument Lists.
By: Chrysanthus Date Published: 12 Nov 2018
Introduction
Recursive Function
A recursive function is a function that keeps calling itself until a condition is met. Try the following code:
<?php
function fnRecu($a)
{
if ($a < 20)
{
echo "$a ";
$b = $a + 1;
fnRecu($b);
}
}
fnRecu(0);
?>
The output is:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The function is called with an argument of 0. While the argument is less than 20, it is echoed for each execution of the function. For each execution, 1 is added to the argument and then the function is called again (with the sum). The calling stops when the argument to the function is 20.
Note: After at least 100 recalling in PHP, the script would stop running. Infinite recursion is considered a programming error in PHP. Comment at the bottom of this tutorial, and I will tell you how you can work-around (do not forget that you have to register, before you can comment).
Solution: do not code any recursive function that has more than 100 recursions. This is a limitation in PHP, which I think, can be worked-around still in PHP (comment below).
Variable-Length Argument Lists
Try the following code:
<?php
function sum(...$numbers)
{
foreach ($numbers as $n)
{
echo "$n ";
}
}
sum(1, 2, 3, 4);
?>
The output is:
1 2 3 4
The variable-length argument list scheme allows you to pass different number of arguments, in different calls, using one parameter. The different values will become consecutive members of the argument list. The scheme uses the token, ... in the parameter parentheses. It can also use the token in the calling argument, list as in the following code:
<?php
function add($a, $b, $c)
{
$z = $a + $b + $c;
echo $z;
}
$d = [10, 20, 30];
add(...$d);
?>
The output is:
60;
Here, the number of arguments is 1 (with ...), while there are 3 parameters
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links