PHP Function Parameter
In this chapter you will learn:
- What is PHP Function Parameter
- Syntax for PHP Function Parameter
- Example - Add parameter to a function
Description
PHP functions can optionally accept one or more arguments, which are values passed to the function.
A parameter is a variable that holds the value passed to it when the function is called.
Syntax
To specify parameters for your function, insert one or more variable names between the parentheses, as follows:
function myFunc( $oneParameter, $anotherParameter,... ) {
// (do stuff here)
}
Example
We need to give each parameter a name to refer to it inside the function. PHP will copy the values it receives into these parameters:
<?PHP//j a va2 s .co m
function multiply($num1, $num2) {
$total = $num1 * $num2;
print $num1;
print "\n";
print $num2;
return $total;
}
$mynum = multiply(5, 10);
echo $mynum;
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is PHP Function Default Parameters
- Syntax for PHP Function Default Parameters
- Example - Create and use default parameter
Home » PHP Tutorial » PHP Function Create