PHP supports variable-length argument function. It means you can pass 0, 1 or n number of arguments in the function. To do so, you need to use 3 ellipses (dots) before the argument name.
A simple example of a PHP variable-length argument function.
<?php
function addCal(...$numbers) {
$sum = 0;
foreach ($numbers as $n) {
$sum += $n;
}
return $sum;
}
echo add(1,2,3,4);
?>
Output:10
int func_num_args(void)
<?php
function callMe()
{
$value = func_num_args();
echo "Number of arguments : ".$value;
}
callMe(1,2,3,4);
?>
Output :
Number of arguments : 4
mixed func_get_arg(int arg_num)
<?php
function callMe()
{
$num_arg = func_num_args();
echo "Number of arguments : ".$num_arg."<br/>";
if($num_arg >= 2)
{
echo "Seccond arguments is : ".func_get_arg(1);
}
}
callMe(1,2,3,4);
?>
Output :
Number of arguments : 4
Seccond arguments is : 2
array func_get_args(void)
<?php
function callMe()
{
$num_args = func_num_args();
if($num_args > 0)
{
$num = func_get_args();
foreach($num as $value)
{
echo $value."<br/>";
}
}
}
callMe("Hello","World");
?>
Output :
Hello
World
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning