HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
functionDefault parameterArrow function ( fn )
🔄 Type & Conversion
PHP›REFERENCE›Function
🧩

Function

Defining your own functions.

CommandDescription
functionDefines a function.
Default parameterGives a parameter a default value.
Arrow function ( fn )A short one-line function.

function

Try it yourself →

Defines a function.

Syntax

function name($p) { return value; }

Description

Defined with function, called by name; takes parameters, returns a result with return.

Example

<?php
function square($x) {
  return $x * $x;
}
echo square(5);

Expected output

25

When to use?

Preventing code repetition.

Common mistakes

  • ⚠️ If there is no return, the function returns NULL.

Default parameter

Try it yourself →

Gives a parameter a default value.

Syntax

function name($x, $y = default) { }

Description

If no argument is provided, the default is used.

Example

<?php
function greet($name, $message = "Hello") {
  return "$message $name";
}
echo greet("Alex");

Expected output

Hello Alex

When to use?

Optional arguments.

Arrow function ( fn )

Try it yourself →

A short one-line function.

Syntax

fn($p) => expression

Description

Short function with fn() => ... (PHP 7.4+). Used in places like array_map.

Example

<?php
$square = fn($x) => $x * $x;
echo $square(6);

Expected output

36

When to use?

Quick transformations, arguments to array methods.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code