PHP Function Parameter

In this chapter you will learn:

  1. What is PHP Function Parameter
  2. Syntax for PHP Function Parameter
  3. 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:

  1. What is PHP Function Default Parameters
  2. Syntax for PHP Function Default Parameters
  3. Example - Create and use default parameter
Home » PHP Tutorial » PHP Function Create
PHP Function
PHP Function Create
PHP Function Parameter
PHP Function Default Parameters
PHP Variable Length Parameter
PHP Function Reference Parameter
PHP Function Return
PHP Return Reference
PHP Variable Scope in Functions
PHP Global Variables
PHP Recursive Functions
PHP Anonymous Function
PHP static variable