Define a PHP Function That Takes Arguments and Returns a Value
Write a PHP function that multiplies two inputs, returns the result, and lets the caller use it. See where parameter and return types go.
To reuse a calculation in PHP, declare a named function with parameters for the inputs, calculate a result, and send it back with return. The caller supplies arguments and can assign the returned value to a variable. That keeps the calculation in one place while letting each call choose its own numbers.
Declare and call a function that calculates a line total
Suppose a line total is the unit price multiplied by the quantity. Here is a complete example:
<?php
function lineTotal($unitPrice, $quantity) {
return $unitPrice * $quantity;
}
$total = lineTotal(12, 3);
echo $total;
$secondTotal = lineTotal(7, 4);
The declaration starts with function, followed by the name lineTotal, a parameter list in parentheses, and a body in braces. That is the structure described by the PHP manual. In this declaration, $unitPrice and $quantity are the parameters: names for the two inputs the calculation will use.
The first call supplies 12 and 3. Their product is 36, so echo $total; displays 36. The second call supplies 7 and 4, producing 28 in $secondTotal. This example does not echo the second result; it leaves that value available to the calling code. If you want to display it too, add echo $secondTotal; where you want that output.
Trace the values from caller to result
Read the first call from right to left once, then follow the values through the function:
lineTotal(12, 3)is the call.12and3are its arguments, the values chosen by the caller.- In the function declaration,
$unitPriceand$quantityare parameters. For this call, they receive12and3, respectively. Parameters are named in the definition; arguments are supplied at the call, as the functions guide distinguishes. return $unitPrice * $quantity;calculates36and sends that value back to the caller. Thereturnstatement ends the function and hands back its value, as described in the Guru99 functions guide.$total = lineTotal(12, 3);assigns the returned36to$total. The followingechouses that variable.
Now make the same trip through lineTotal(7, 4). Nothing in the function declaration changes. The caller changes the arguments, so the calculation yields 28, which the assignment places in $secondTotal. This is why returning the product is useful: the function calculates it, but the caller decides what to do with it.
Put optional type declarations in the signature
The first version leaves its parameter and return types undeclared. If you want to declare integer inputs and an integer result, replace that declaration with this one:
function lineTotal(int $unitPrice, int $quantity): int {
return $unitPrice * $quantity;
}
The int before each parameter name is a parameter type declaration. The : int after the closing parenthesis is the return type declaration; that placement is documented in the PHP functions lesson. PHP 7 added type declarations, so PHP 7 is the supported version boundary for this typed example, according to the PHP 7 functions guide.
Use this as an alternative declaration, not as a second declaration of lineTotal alongside the first. The calls can still read lineTotal(12, 3) and lineTotal(7, 4). Type declarations describe the expected inputs and result; they do not replace the arguments at the call or the return statement in the body.
