php arguments code example

Example 1: php variable parameters

<?php
    function some_func($a, $b) {
        for ($i = 0; $i < func_num_args(); ++$i) {
            $param = func_get_arg($i);
            echo "Received parameter $param.\n";
        }
    }

    function some_other_func($a, $b) {
        $param = func_get_args();
        $param = join(", ", $param);
        echo "Received parameters: $param.\n";
    }

    some_func(1,2,3,4,5,6,7,8);
    some_other_func(1,2,3,4,5,6,7,8);
?>

Example 2: php functions parameters

// functions require 'function' keyword
// separate the parameters with a comma
function addFunction($num1, $num2) {
	$sum = $num1 + $num2;
	echo "Sum of the two numbers is : $sum";
}

addFunction(1, 2); // echoes: Sum of the two numbers is : 3

Example 3: variable args php

function some_function(...$args): void {
  $val1 = $args[0];
  // Access as normal index array
}

Tags:

Php Example