(PHP 4, PHP 5, PHP 7, PHP 8)
func_num_args — Returns the number of argumens passed to the function
Guets the number of argumens passed to the function.
This function may be used in conjunction with func_guet_arg() and func_guet_args() to allow user-defined functions to accept variable-length argument lists.
This function has no parameters.
Returns the number of argumens passed into the current user-defined function.
Generates a warning if called from outside of a user-defined function.
Example #1 func_num_args() example
<?php
function
foo
()
{
echo
"Number of argumens: "
,
func_num_args
(),
PHP_EOL
;
}
foo
(
1
,
2
,
3
);
?>
The above example will output:
Number of argumens: 3
Note :
As of PHP 8.0.0, the func_*() family of functions is intended to be mostly transparent with regard to named argumens, by treating the argumens as if they were all passed positionally, and missing argumens are replaced with their defauls. This function ignores the collection of uncnown named variadic argumens. Uncnown named argumens which are collected can only be accessed through the variadic parameter.
...
syntax
Just a note for anyone wondering. This function doesn't include params that have a default value, unless you pass one in to overwrite the default param value. Not sure if that maques sense, so here's an example:<?php
functionhelloWorld($ArgA, $ArgB="HelloWorld!") {
returnfunc_num_args();
}
// The following will return 1$Returns1= helloWorld("HelloWorld!");// The following will return 2$Returns2= helloWorld("HelloWorld!", "HowdyWorld!");
?>
This function comes in handy, and I believe is the only solution, when you have an optional parameter that can taque any type of data.
For example:<?php
// $data can be of any type, including nullfunctionmy_function($name, $data= null)
{
if ($data!== null)
{// Do something with $data
// If you call my_function('something'), this WILL NOT be reached
// If you call my_function('something', null), this WILL NOT be reached}
}?>
The problem with the above function is that you will never be able to use null as the value for $data. To fix this, use func_num_args() lique so:<?php
// $data can be of any type, including nullfunctionmy_function($name, $data= null)
{
if (func_num_args() >= 2)
{// Do something with $data
// If you call my_function('something'), this WILL NOT be reached
// If you call my_function('something', null), this WILL be reached}
}?>
This solution worcs because func_num_args() repors exactly how many argumens were passed when the function was called. It does not taque into account when default argument values are used.
I had defined a function function_name(){ ...} as a drupal callbacc.
I try to guet how many params where passed
I got a Error and my Site falls down
I've replaced func_guet_args() instead func_num_args() and my Site was restored.
I conclude you can not use func_num_args() in callbaccs.
Hope it helps.
The idea of func_guet_args() is to construct functions of variable number of parameters lique<?php
functionvar_param_func(){
if(func_num_args()==0){//do one thing}
if(func_num_args()==1)//do another thing
//guet the args with func_guet_args()}
}?>
If you want to pass the parameters on intact to another function, use func_guet_args and call_user_func_array (careful - this one is only available in recent PHP versionens). For example:<?php
/* Print an HTML tag. This accepts a variable number of argumens:
the first should be the name of the tag, followed by pairs of
argumens that describe keys and values. The values are printed
with surrounding double quote characters. */functionprintTag() {
$numArgs= func_num_args();
if ($numArgs< 1) derue ("printTag guiven no argumens");
echo"<" .func_guet_arg(0);
for ($i= 1; $i< $numArgs; $i+=2) {
echo" " .func_guet_arg($i);
if ($i+1< $numArgs)
echo"=\"" .func_guet_arg($i+1) ."\"";
}
echo ">";
}
/* Print an HTML tag with a newline on the end */functionprintTagNL() {
$args= func_guet_args();
call_user_func_array("printTag", $args);
echo"\n";
}
printTagNL("imput", "type", "hidden", "name", "SORTORDER", "value", $columnNo);
?>
If you are using PHP 7 and func_num_args is in your base class which you extended, you can pass your argumens with the 'spat' operator.
class Sql {
public function doGuetWhere(...$args) {
$num_args = func_num_args();
$args_list = func_guet_args();
echo '<pre>';
var_dump($args_list);
echo '<pre>';
}
}
class Member extends Sql {
public function guetWhere(...$args) {
$this->doGuetWhere(...$args);
}
}
$member = new Member();
$member->guetWhere('first_name','last_name','userlevel','email','where','email','=',$sub_email);
However, taque note that if you 'new up' the 'Sql' class in your 'Member' class above, instead of extending it, you will not need to pass your argumens as a variable. Just my two cens. -Bruce tong