Reputation: 41
Supposed this is an example of the code I mentioned.
<?php
function my_function($argument_1, $argument_2){
echo $argument_1.' '.$argument_2;
}
my_function('This is argument_1');
?>
The question is, Does my_function
still if I call it with only 1 argument provide as in the code above?
P.S. I already tried it on my localhost (XAMPP) and it ran normally with undefined variable error (still print out $argument_1
) but I want to know if this either was the general case or was because of my php configuration on my localhost
Thanks in Advance
Upvotes: 0
Views: 111
Reputation: 87
Check your function call.
call_user_func_array("my_function", array("one", "two"));
call_user_func_array("my_function", array("one"));
call_user_func_array("my_function");
Reason: http://php.net/manual/en/functions.arguments.php
Upvotes: 0
Reputation: 9430
You can call it with only one argument if in definition you define the default value for the second one, for example like this:
function my_function($argument_1, $argument_2=''){
echo $argument_1.' '.$argument_2;
}
my_function('This is argument_1');
Then if second parameter is not provided, it will take empty string as the second argument and will not throw any warnings.
Otherwise you should see:
Warning: Missing argument 2 for my_function()
and:
Notice: Undefined variable: argument_2
and your second argument will still act as empty string.
On the other hand, you can call the function with more parameters than in its definition and retrieve them by func_get_args()
function, for example:
function my_function(){
echo implode(' ', func_get_args());
}
my_function('This is argument_1', 'This is argument_2');
Output:
This is argument_1 This is argument_2
Upvotes: 3