X
    Categories: PHP

func_get_arg, func_get_args , func_num_args in php

func_get_arg, func_get_args , func_num_args in php

func_get_arg, func_get_args functions are used to get a set of arguments which are passed to the functions. The arguments can be a integer, string, array or anything.

Basically func_get_arg and func_get_args will collect the arguments in array.

func_get_arg

func_get_arg will return the single value of the set of arguments based on key values.

 

<?php 

class funGetArgs
{
	function test()
	{
	    $s = func_get_arg(1);
	    return $s;
	}
}
$abc = new funGetArgs;
$result = $abc->test('php', 'laravel', 1, ['css', 'html']);
print_r($result);

OutPut : laravel

 func_get_args

func_get_args will return all the arguments in array format.

<?php 

class funGetArgs1
{
	function test()
	{
	    $s = func_get_args();
    	return $s;
	}
}

$abc = new funGetArgs1;
$result = $abc->test('php', 'laravel', 1, ['css', 'html']);
print_r($result);

OutPut :  Array ( [0] => php [1] => laravel [2] => 1 [3] => Array ( [0] => css [1] => html ) )
?>

 func_num_args

func_num_args are used to count the number of arguments passing to the functions.

<?php 

class funGetArgs3
{
	function test()
	{
	    $s = func_num_args();
	    $a = func_get_args();
	    for($i = 0; $i < $s; $i++)
	    {
	        $arg = is_array(func_get_arg($i)) ?  implode(",", func_get_arg($i)) : func_get_arg($i);
	        echo "Number: " . $i . " argument is " . $arg . "<br />\r\n";
	    }
	     
	    echo "<br />\r\nThere are " . $s . " arguments<br />\r\n";
	    echo "<br />\r\nThe array: <br />\r\n";
	    print_r($a);
	     
	}
}

$abc = new funGetArgs3;
$result = $abc->test('php', 'laravel', 1, ['css', 'html']);
print_r($result);

//OutPut : 
Number: 0 argument is php
Number: 1 argument is laravel
Number: 2 argument is 1
Number: 3 argument is css,html

There are 4 arguments

?>

Thanks for reading this article. If you like this article, Please don’t forget to share and comment.

Thanks!.

Marimuthu: