Syntax
function greet(string $name): string {
return "Hello, $name!";
}Examples
PHP function definitions example
A focused example you can adapt while practising.
function greet(string $name): string {
return "Hello, $name!";
}Best practices
- Start with the smallest working example, then adapt it to your project.
- Use the language’s standard library and idioms before introducing extra dependencies.
At a glance
- Purpose
- Server-side web applications
- File extension
- .php
- Runs in
- PHP runtime on a server or CLI
- Usually used with
- HTML, databases and web servers
Specifications & further reading
Related PHP documentation
PHP Functions & Type Declarations
PHP functions use the function keyword. Parameter and return type declarations can document and enforce the values a function accepts and returns.PHP union types
PHP reference for php union types.PHP default parameters
PHP reference for php default parameters.PHP named arguments
PHP reference for php named arguments.
PHP functions use the function keyword. Parameter and return type declarations can document and enforce the values a function accepts and returns.PHP union types
PHP reference for php union types.PHP default parameters
PHP reference for php default parameters.PHP named arguments
PHP reference for php named arguments.