Web Development & WordPress

How to Handle Dynamic Parameters in PHP: Working with Unknown Number of Function Arguments

By using func_get_args(), you can capture and handle any number of parameters passed to a function, making your PHP functions more flexible and versatile.

<?php

function getName()
{

    $names = func_get_args();
    echo "<br>Showing the names using print_r() function<br>";
    echo "<pre>";
    print_r($names);
    echo "</pre>";

    echo "<br>Showing the names using foreach: <br>";
    foreach ($names as $name) {
        echo $name . "<br>";
    }
}
getName("Tom", "Jim", "Harry", "Torry");
?>

OUTPUT

Showing the names using print_r() function
Array
(
    [0] => Tom
    [1] => Jim
    [2] => Harry
    [3] => Torry
)

Showing the names using foreach:
Tom
Jim
Harry
Torry

Leave a comment