codegurukool.com · Free coding tutorials & online compiler

Home › PHP Tutorial › PHP functions

PHP functions

Reuse code with named blocks.

Functions can have typed parameters, default values and a return type. Arrow functions (fn) are a short form for one-line functions.

Example code

<?php
function greet(string $name, string $greeting = "Hello"): string {
  return "$greeting, $name!";
}
echo greet("Ada") . PHP_EOL;
echo greet("Bob", "Hi") . PHP_EOL;
$sq = fn($n) => $n * $n;
echo $sq(5);
?>

Run this code in the online editor

← PHP loopsPHP arrays →