Functions in PHP (with Examples)
In this lesson: Create PHP functions with parameters, default values, return, type hints and arrow functions — examples with output.
What is a function
A function is a named block of code that does one job. You write it once, then call it whenever you need that job done. This keeps your program short and avoids repeating the same lines everywhere.
strlen() and echo, and you can make your own too.Define and call
Use the function keyword to define, then call it by its name with brackets.
<?php
function sayHello() {
echo "Hello, welcome!";
}
sayHello(); // calling it
?>
Defining a function does not run it — it only runs when you call it. You can call the same function as many times as you like.
Parameters and return
Parameters let you pass values in. return sends a result back.
<?php
function add($a, $b) {
return $a + $b;
}
$sum = add(5, 3);
echo $sum;
?>
Here $a and $b are parameters; 5 and 3 are the arguments. return hands back 8, which we store in $sum.
Default parameters
Give a parameter a default value so it is optional.
<?php
function greet($name = "Guest") {
echo "Hi, $name";
}
greet("Aman"); // Hi, Aman
echo "\n";
greet(); // Hi, Guest
?>
Hi, Guest
When no argument is given, the default ("Guest") is used. Defaults must come after non-default parameters in the list.
Scope inside functions
Variables inside a function are local — they do not affect variables outside.
<?php
$x = 10;
function change() {
$x = 99; // a separate local $x
}
change();
echo $x; // still 10
?>
The function's $x is a different box from the outside $x. To change an outside value, return it and reassign, or pass by reference — the clean way is usually to return.
Common mistakes
- Defining but never calling. A function does nothing until you call it.
- Forgetting
return. Without it the function gives back NULL, so$sumwould be empty. - Wrong argument order. Arguments fill parameters left to right — order matters.
- Expecting inside changes to affect outside variables. They are local unless returned or passed by reference.
Summary
- A function is reusable named code — define once, call many times.
- Parameters pass values in;
returnsends a result back. - Default parameters make an argument optional.
- Variables inside a function are local by default.
- Functions keep code short, clear and easy to fix.
Next, use functions on text with PHP string functions, or organise data with arrays in PHP.
Frequently Asked Questions
What is the benefit of using functions?
What is the difference between a parameter and an argument?
function add($a, $b)). An argument is the actual value you pass when calling it (like add(5, 3)). Parameter is the placeholder; argument is the real value.What does return do in a function?
return sends a value back to wherever the function was called, and ends the function immediately. Without return, a function does its work but gives back nothing (NULL).What is a default parameter?
function greet($name = "Guest"), calling greet() uses "Guest" automatically, while greet("Aman") uses "Aman".Can a function use a variable from outside it?
global keyword.