How to use functions in PHP
One of the first things you’ll learn in most coding languages is how to write your own functions.
Coding languages have built-in functions which you call from your code like this.
<?php
echo date("d/m/Y");
?>
But what if there isn’t a built-in function that does what you need it to do?
Or, what if you want to use several built-in functions in succession in a uniform way and repeat exactly the same steps over and over again?
Well, this is what building your own functions is for and the syntax is really simple.
Basic syntax
Here’s how you’d define and call a function.
<?php
// Defining a function
function myFunction() {
echo "Hello world!";
}
// Calling a function
myFunction();
?>
Our function is called myFunction. We set it and then we call it. It outputs Hello world!.
On naming, it can start with a letter or an underscore, followed by any number of letters, numbers or underscores.
Output or return
You can either output directly from the function or return a value.
The first code example above shows a direct output and here’s an example of returning a value from the function to the call.
<?php
// Defining a function
function myFunction() {
return "Hello world!";
}
// Calling a function
echo myFunction();
?>
Introducing paramaters
Next up, let’s include some parameters. These are configurable options set in the call and then are used in the function itself.
<?php
// Defining a function
function myFunction(parameter1, parameter2) {
return parameter1 . " " . parameter2;
}
// Calling a function
echo myFunction("One", "Two");
// Output: `One Two`
?>
When we call the function, we set parameter1 and parameter1 to One and Two respectively.
Then, in the function we concatenate the two parameters with a space in between and return it to the call, where it is echoed.
Default parameter values
You may not always want to set the parameter every time you call the function.
You can set default values like this.
<?php
// Defining a function
function myFunction(compulsory, optional = null) {
return compulsory . " " . optional;
}
// Calling a function
echo myFunction("One");
// Output: `One `
?>
You don’t have to set the optional parameter as null, you could use any value – a number, a string, whatever you like.
Parameter type
You don’t have to but it might pay off later in your function code – we can set parameter type.
<?php
// Defining a function
function myFunction(string compulsory, int optional = 1) {
return compulsory . " " . optional;
}
// Calling a function
echo myFunction("One");
// Output: `One 1`
?>
Variable scope
In PHP, you can’t access a variable from within a function if it was set from outside a function. Like this:
<?php
$myVariable = "something";
function myFunction() {
echo $myVariable;
}
// Calling a function
echo myFunction();
?>
Here’s another.
<?php
function myFunction() {
$myVariable = "something";
}
// Calling a function
myFunction()
// Accessing a variable (doesn’t work)
echo $myVariable;
?>
The functions above do not work.
Here are a few examples of what does work.
<?php
$myVariable = "something";
// Passing the variable as a parameter
function myFunction($myVariable) {
return $myVariable;
}
myFunction($myVariable)
// Accessing the variable as a global
function myFunction() {
return $GLOBALS["myVariable"];
}
myFunction()
?>
Passing by value and by reference
By default, your function receives the variable value (passed by value), which means you can’t directly change the value from within the function.
<?php
// Define the variable
$myVariable = "something";
// Define the function
function myFunction($myVariable) {
$myVariable = "something else";
}
// Call the function
myFunction($myVariable);
// Output the variable
echo $myVariable;
// Outputs: `something` (not `something else`)
?>
Here’s the small modification required to get the above code working.
<?php
// Define the variable
$myVariable = "something";
// Define the function -- changed
function myFunction($v) {
return $v . " else";
}
// Call the function -- changed
$myVariable = myFunction($myVariable);
// Output the variable
echo $myVariable;
// Outputs: `something else` (not `something`)
?>
Note the paramater name doesn’t need to be the same as the function (where it’s defined in the example above).
To pass a variable by reference and be able to amend, change and manipulate it from within a function, we do this.
<?php
// Define the variable
$myVariable = "something";
// Define the function
function myFunction(&$v) {
$v = "something else";
}
// Call the function
myFunction($myVariable);
// Output the variable
echo $myVariable;
// Outputs: `something` (not `something else`)
?>
Note again the paramater name doesn’t need to be the same as the function (where it’s defined in the example above).
You could also redefine the variable via $GLOBALS["myVariable"] = "new value".
Named functions
Here’s the basic syntax for named functions.
<?php
// Define the arrow function
$myFunction = function() {
// function code
};
// Call the arrow function
$myFunction();
?>
The use key let’s us bring in globally defined variables.
<?php
$myVariable = "something";
$myFunction = function() use ($myVariable) {
echo $myVariable;
};
echo $myFunction();
?>
Arrow functions
Just like you can in JavaScript, you can use arrow functions in PHP too.
<?php
// Define the function
$average = fn($a, $b) => ($a + $b) / 2;
// Call the function
echo $average(10, 20)
// Outputs: 15
?>
And that’s it ... I think ... everything you need to know about PHP functions.
