Function Multiple arguments in PHP

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: PHP

In PHP, we can use multiple arguments in a method or function to pass multiple values to be used in the function body. Here is an example of how to use multiple arguments in PHP:

function add($num1, $num2) {
    $result = $num1 + $num2;
    return $result;
}

echo add(2, 3); // Output: 5

In this example, we define a function called add that takes two arguments $num1 and $num2. The function adds these two numbers and returns the result.

We can also use an arbitrary number of arguments in a function using the func_get_args() method. Here is an example:

function sum() {
    $args = func_get_args();
    $total = 0;
    foreach($args as $arg) {
        $total += $arg;
    }
    return $total;
}

echo sum(1, 2, 3, 4); // Output: 10

In this example, we define a function sum that takes an arbitrary number of arguments using the func_get_args() method. The function calculates the sum of all arguments using a foreach loop.

These are just a couple of examples of how to use multiple arguments in a method or function in PHP. By passing multiple values to a function, we can make our code more flexible and versatile.