PHPFunctions Introduction

Functions Introduction

A function is a named block of code you can call by name instead of retyping it every time you need it. PHP has hundreds of built-in functions like strlen() or array_map(), but the real power comes from writing your own. Once logic is wrapped in a function, you can call it from anywhere in your script, pass it different inputs, and get predictable outputs back — without copying and pasting the same ten lines in five different places.

The function keyword and basic syntax

You define a function with the function keyword, a name, a parenthesized list of parameters, and a body wrapped in curly braces. Calling it later just means writing the name followed by parentheses with whatever arguments it expects.

A basic function

PHP
<?php
function greet($name) {
    return "Hello, {$name}!";
}

echo greet("Priya");
Hello, Priya!

Function names in PHP are case-insensitive (unlike variables), follow the same character rules as variable names minus the dollar sign, and by convention are written in camelCase or snake_case depending on the coding style your project follows.

Why bother with functions at all

The main reason to reach for a function is the DRY principle — Don't Repeat Yourself. If you find yourself writing the same calculation, validation, or formatting logic more than once, that logic belongs in a function. Beyond avoiding duplication, functions give you:

  • A single place to fix a bug — fix it once in the function, and every caller benefits.

  • A meaningful name for a chunk of logic, which documents intent better than a bare comment.

  • A natural unit for testing — you can call a function with known inputs and check the output in isolation.

  • A way to keep the global scope clean, since variables created inside a function do not leak out (covered in depth on the Variable Scope page).

Calling a function before its definition (top-level "hoisting")

Here is a quirk that surprises people coming from JavaScript or Python: at the top level of a PHP file, function definitions are processed before the script actually starts executing line by line. This means you can call a top-level function earlier in the file than where it is textually defined, and it still works.

Calling before the textual definition

PHP
<?php
echo square(5);

function square($n) {
    return $n * $n;
}
25

This works because PHP does a first pass over the file and registers every unconditionally declared function, then runs the script. It is not true hoisting in the JavaScript sense (variables are not hoisted), but for plain top-level function declarations, the effect is similar.

The exception: conditionally defined functions

That "hoisting" behavior only applies to functions declared unconditionally at the top level of a file. If a function is defined inside an if, a loop, or any other conditional block, PHP only registers it once that block actually executes — so you must call it after the definition has run, not before.

A conditionally defined function must be called after definition

PHP
<?php
$debugMode = true;

if ($debugMode) {
    function logMessage($msg) {
        echo "[DEBUG] {$msg}";
    }
}

// This works because the if-block above already ran
logMessage("Cache cleared");
[DEBUG] Cache cleared
Calling a conditional function too early fails
If the `logMessage("Cache cleared");` call above were moved *before* the `if` block, PHP would throw a fatal error along the lines of `Call to undefined function logMessage()`, because the function does not exist yet at that point in execution. Only unconditional, top-level function declarations get the "define anywhere in the file" convenience.
Functions calling functions

Functions can freely call other functions, including ones defined later in the file, as long as the call itself happens after the script has had a chance to register all top-level definitions (which, practically speaking, means "after the whole file has been parsed").

One function using another

PHP
<?php
function celsiusToFahrenheit($c) {
    return $c * 9 / 5 + 32;
}

function describeWeather($celsius) {
    $f = celsiusToFahrenheit($celsius);
    return "{$celsius}C is {$f}F";
}

echo describeWeather(20);
20C is 68F
Return value is optional
A function does not have to `return` anything. If it never hits a `return` statement, calling it evaluates to `null`. This is common for functions whose entire job is a side effect, like printing output or writing to a file.
Tip
Keep each function focused on one job. If you catch yourself naming a function `processAndSaveAndEmail()`, that is usually a sign it should be split into three smaller functions — each easier to read, test, and reuse independently.