PHP Function Parameter

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/* w  ww .  j  a  v  a 2  s  .  c o 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.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition