How to create and call a function in PHP that takes arguments or parameter?
The following is the syntax that creates a function:
function function_name ($arg1, $arg2, …) {
statement(s); // statements in the function
…
Return $variable;
}
// example of returning multiple values…
function function_name ($arg1, $arg2, …) {
statement(s); // statements in the function
…
Return array( $value1, $value2, …);
}
// example of using global variable…
// The global variable exists outside of the function, it
// also have the same value within the function.
function function_name ($arg1, $arg2, …) {
global $variable;
statement(s); // statements in the function
…
Return array( $value1, $value2, …);
}
The following is the syntax that calls a function:
function_name($variable1, $variable2, …);
-- or --
function_name(‘literal value1’, ‘literal value2’, …);
-- or --
function_name(‘mixed literals and variables’, $variable, …);
|