Functions: Write Once, Use Everywhere

Bundling code into reusable, named recipes

9 min read

Picture a store that shows prices in twenty places: product pages, the cart, receipts, emails. Each time, the price needs a currency symbol and two decimal places. Copy-pasting the same formatting code twenty times works, right up until the day you need to change it, and then you have twenty places to fix and you will forget one. There is a better way: write it once, give it a name, and call it by name everywhere. That is a function.

Defining and calling a function

<?php
function sayHello() {
    echo "Hello there!";
}
sayHello();
sayHello();
?>

The function keyword starts the definition, then comes the name you choose, parentheses, and the body in curly braces. Important: defining a function runs nothing. It just teaches PHP the recipe. The code only runs when you call the function by writing its name with parentheses. This example calls it twice, so it prints Hello there! twice.

Parameters: giving a function input

Functions become properly useful when you can hand them different values each time:

<?php
function greet($name) {
    echo "Hello, " . $name . "!";
}
greet("Ada");
greet("Grace");
?>

$name is a parameter: a placeholder variable that gets filled in with whatever value you pass in the call. The first call prints Hello, Ada! and the second prints Hello, Grace!. Same recipe, different ingredients.

return: giving an answer back

Instead of echoing directly, most functions compute something and hand the result back with return:

<?php
function formatPrice($amount) {
    return "$" . number_format($amount, 2);
}
$label = formatPrice(1234.5);
echo $label;
echo formatPrice(8);
?>

This prints $1,234.50 and then $8.00. The returned value flows back to wherever the call happened, so you can store it in a variable, echo it, or pass it into another function. Two notes: number_format is one of thousands of functions PHP ships with ready-made, and the moment a function hits return, it stops. Nothing after the return line runs.

Why this changes everything

Now the store scenario has a happy ending. Prices formatted in twenty places, but the recipe lives in exactly one. Boss wants euros instead of dollars? Edit one line inside formatPrice, and all twenty places update instantly.

  • One place to fix bugs. Correct the function, and every caller is corrected.
  • Readable code. formatPrice($total) says what it does; ten lines of raw formatting do not.
  • Building blocks. Big programs are just small functions calling each other.

Variables created inside a function live only inside it; the outside world cannot see them. This is called scope, and it is a feature: functions keep their mess in their own room, so they never accidentally trample your other variables.

Quick check

Q1 What does the return statement do inside a function?

Q2 Why are functions better than copy-pasting the same code in many places?

Want to save your progress? It's free.

Create a free account