Functions4 min readExamples run on PHP 8.5.10

PHP Functions: Parameters and Return Values

Give a calculation a name, supply arguments when calling it, and return a result that other code can use.

Define and call

The function keyword begins a named function declaration. Parentheses contain its parameters; braces contain the body. Calling the function supplies argument values for those parameters.

Return a result

return sends a value to the caller and finishes the call. Save that result in a variable, pass it to another function or print it with echo. Printing and returning serve different purposes.

Keep the contract clear

Parameter and return type declarations describe the values a function accepts and returns. Type coercion and strict typing have further rules; the official manual explains them in detail.

Tested examples

Minimal working example
<?php function area(int $width, int $height): int { return $width * $height; } echo area(3, 4);
Output
12