This post covers the Arguments section of the Functions chapter when studying for the Zend PHP 7 Certification.
Information may be passed to functions via the argument list, which is a comma-delimited list of expressions.
<?php
function somefunction($arg1, $arg2, $arg3) {
// Do something
}
Arguments are evaluated from left to right. This means that the below example would output abc
.
<?php
function myFunction($x, $x = 1, $x = 2, $x = "abc") {
return $x;
}
echo myFunction(0); // abc
The func_num_args()
function returns the number of arguments that are passed to the function.
<?php
function foo() {
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3, 'test'); // Outputs: Number of arguments: 4
This function can be used in conjunction with func_get_arg()
and func_get_args()
func_get_arg()
will get the specified argument from a user-defined function’s argument list.
mixed func_get_arg ( int $arg_num )
Where $arg_num
is the argument offset. Function arguments are counted starting from zero.
<?php
function foo() {
echo "Second argument is: " . func_get_arg(1);
}
foo(1, 2, 3); // Outputs: Second argument is: 2
The func_get_args()
will return an array comprising a function’s argument list.
<?php
function foo() {
$arg_list = func_get_args();
print_r($arg_list);
}
foo(1, 'abc', array()); // Outputs: Array ( [0] => 1 [1] => abc [2] => Array ( ) )
Note that passing by arguments by reference will change the output of func_get_args()
if the argument is altered within the function.
Passing by value:
<?php
function foo($x, $y, $z)
{
$y = 'def';
$arg_list = func_get_args();
print_r($arg_list);
}
$str = 'abc';
foo(1, $str, array()); // Outputs: Array ( [0] => 1 [1] => abc [2] => Array ( ) )
Passing $y
by reference:
<?php
function foo($x, &$y, $z) {
$y = 'def';
$arg_list = func_get_args();
print_r($arg_list);
}
$str = 'abc';
foo(1, $str, array()); // Outputs: Array ( [0] => 1 [1] => def [2] => Array ( ) )
By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function).
To allow a function to modify its arguments, they must be passed by reference.
To have an argument to a function always passed by reference, prepend an ampersand, &
, to the argument name in the function definition.
Passed by value:
<?php
function add_some_extra($string) {
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // Outputs: This is a string,
Passed by reference:
<?php
function add_some_extra(&$string) {
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // Outputs: This is a string, and something extra.
Type declarations allow functions to require that parameters are of a certain type at call time.
<?php
function test(boolean $param) {}
test(true);
If the given value is of the incorrect type, then a TypeError
exception is thrown. More on type declarations can be found in the Type Declarations section.
View the other sections:
Note: This article is based on PHP version 7.1.