Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functions
php programming
communitycreator

What is the func_num_args() function in PHP?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Functions are code blocks which perform a specific function in a software program code. These functions are mostly written by developers to avoid repeating themselves.

In PHP in particular, functions are declared with the following syntax:

function name_of_Function(args){
//code to be executed;
}

Upon calling the function, which can be done like so:

name_of_function(args);

the action to be carried out in the function is performed and it can produce an output, or not.

When these PHP functions are called, they can be provided with arguments greater than or equal to the number of arguments that were initialized. However, the arguments they are provided with cannot be less than the number of arguments that were initialized, because then a fatal error will be thrown.

Let’s look at the following example to better understand this:

function name_of_Function(args1,args2){
//code to be executed;
}
//correct call
name_of_function(args1,args2,args3);

// wrong call
name_of_function(args1);

The func_num_args() function

In PHP, we can call the func_num_args() function if we wish to return and output the number of arguments that is provided at any point in the use time of a function.

This method returns the number of arguments that are passed to the function.

Syntax

func_num_args();

Just place this in the code block of a function, store the return value in a variable, and output it to the screen.

Parameters

This function accepts no parameter.

Return value

This function returns an integer value, which is the number of arguments that were passed to a function.

Errors and support

This function will throw an error if it is called outside a function we defined in our script. Simply put, we should not call the function outside a function block.

It is supported by PHP 4 and later versions of PHP.

Code example

In the example given below, the func_num_args() function is called in our function, which returns the number of parameters that were passed to the function whenever it was called.

In the first call of our function arguments(), the func_num_args() function detects that three parameters where passed. In the second call, it detects that two parameters were passed.

<?php
function arguments($args1,$args2)
{
$num_args = func_num_args();
echo "This function was given: $num_args parameters\n";
}
arguments(1,3,5);
arguments(1,3);
?>

RELATED TAGS

functions
php programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring