Codeskill

Learn to code, step by step

Functions in PHP: Writing Reusable Code

Onto PHP functions. Reusable blocks of code you define once and call whenever you need them.

What are functions in PHP?

A function is a named block of code that does one job. You write it once, then call it as many times as you like. That keeps scripts shorter, easier to read, and easier to maintain.

Defining a function

Use the function keyword, a name, parentheses, and curly braces {} around the code to run:

<?php
function greet() {
    echo "Hello, PHP world!";
}
?>

This defines a function called greet that prints a greeting.

Calling a function

Defining a function does nothing on its own. Call it by name with parentheses:

<?php
greet(); // Calls the greet function
?>

Output: “Hello, PHP world!”.

Function parameters

Parameters let you pass data into a function so it can work with different values each time:

<?php
function personalizeGreeting($name) {
    echo "Hello, $name!";
}

personalizeGreeting("Alice");
personalizeGreeting("Bob");
?>

$name is a parameter. Each call passes a different value.

Returning values from functions

Use return when a function should send a result back to the caller:

<?php
function add($number1, $number2) {
    return $number1 + $number2;
}

$total = add(5, 10);
echo "Total: $total";
?>

The add function returns the sum of its two arguments.

Default parameter values

You can give parameters default values. If the caller does not pass a value, the default is used:

<?php
function makeCoffee($type = "cappuccino") {
    return "Making a cup of $type.<br>";
}

echo makeCoffee();
echo makeCoffee("espresso");
?>

Calling makeCoffee() with no argument makes a cappuccino. Pass “espresso” to override the default.

Variable scope and functions

Variables declared outside a function cannot be read inside it, and vice versa. That is scope in action:

<?php
$globalVar = "I'm global";

function testScope() {
    $localVar = "I'm local";
    echo $globalVar; // This will cause an error
}

testScope();
echo $localVar; // This will also cause an error
?>

To read a global variable inside a function, declare it with the global keyword:

<?php
function testGlobalScope() {
    global $globalVar;
    echo $globalVar;
}

testGlobalScope();
?>

Anonymous functions

Anonymous functions (closures) have no name. They are often stored in a variable:

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

$greet("World");
?>

Practical example: a simple calculator

Here is a small calculator built from separate functions for each operation:

<?php
function add($num1, $num2) {
    return $num1 + $num2;
}

function subtract($num1, $num2) {
    return $num1 - $num2;
}

function multiply($num1, $num2) {
    return $num1 * $num2;
}

function divide($num1, $num2) {
    if ($num2 == 0) {
        return "Cannot divide by zero!";
    }
    return $num1 / $num2;
}

echo "10 + 5 = " . Add(10, 5) . "<br>";
echo "10 - 5 = " . Subtract(10, 5) . "<br>";
echo "10 * 5 = " . Multiply(10, 5) . "<br>";
echo "10 / 5 = " . Divide(10, 5) . "<br>";
?>

Each operation lives in its own function. The echo lines at the bottom call them with different numbers.

Functions are how you keep PHP scripts organised as they grow. Type out the calculator example and tweak the numbers – once functions click, you will reach for them constantly.

PreviousPHP Loops: The Power of Repetition