Syntax

PHP Define a Function Syntax

In PHP, define a function: defines a typed function — function add(int $a, int $b): int { return $a + $b; }

Quick Answer

Syntax
function add(int $a, int $b): int { return $a + $b; }
PHPSyntax

Define a Function

Code example
function add(int $a, int $b): int { return $a + $b; }

Defines a typed function.

Learn More

How to define a function in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Define a Function Syntax

Use function add(int $a, int $b): int { return $a + $b; }.

Related Syntax References

Frequently asked questions

What is the PHP syntax for define a function?

Use this pattern: function add(int $a, int $b): int { return $a + $b; }

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify