PHP Functions
PHP Functions
This page explains how to create a function in PHP.
Sometimes you might find yourself writing the same piece of code over and over again. You might even find yourself "copy/pasting" code so that you can re-use it in another part of your application. The problem with doing this is, you now have more code to maintain. For example, if you want to change the code, you need to change it in many different places. A better idea would be to put that code into a function.
PHP functions are self contained blocks of code that perform a specified "function", then return a value. A function often accepts one or more "parameters" (also referred to as "arguments") which you can "pass" to it. By providing a different parameter to the function, you get a different result (depending on what the function actually does).
Creating PHP Functions
To create a PHP function, follow these steps:
- Think of a name for the function. Make it short but descriptive. It should describe what the function actually does.
- Type function
- On the same line, type the name of the function followed by opening/closing brackets ( eg, myFunction() ).
- Add an opening curly brace ( { )
- Type the code that makes up the function
- Finish with a closing curly brace ( } )
Example
<?php
function writeMyFavoriteFruit()
{
echo "My favorite fruit is pomegranate.";
}
?>
Calling PHP Functions
The function doesn't actually do anything until you call it. To call a function, do the following:
- Write the function name followed by opening and closing brackets (i.e. functionName() )
- Between the opening/closing brackets, provide any parameters the function requires. If it doesn't require any parameters you can leave it blank.
Example
In this example, we create the same function we created in the previous example. We then call that function:
<?php
function writeMyFavoriteFruit()
{
echo "My favorite fruit is pomegranate.";
}
writeMyFavoriteFruit();
?>
The above code results in the following:
My favorite fruit is pomegranate.
Passing Parameters
The previous example is a very simplistic version of a function. We could improve this function by allowing it to accept a parameter. For example, imagine if, at the time we called the function, we could pass it the name of a fruit. The function could then display the name of that fruit (instead of displaying "pomegranate" every time).
Example 1
<?php
function writeMyFavoriteFruit($fruitName)
{
echo "My favorite fruit is: " . $fruitName;
}
writeMyFavoriteFruit("Watermelon");
?>
The above code results in the following:
My favorite fruit is: Watermelon
This is one of the things that make functions so cool. For example, by accepting a parameter (such as the fruit name), we could let our website users provide us with the name of their favorite fruit. Behind the scenes, we could put that fruit into a variable, then pass that variable to our function. The function could then display the name of their favorite fruit (instead of "pomegranate"!).
Example 2
In this example, the fruit name is supplied as a variable. For the purposes of this tutorial, we set the variable to a hard-coded string, but there's no reason this couldn't be a value provided by the user (for example, a value from a form).
<?php
$userFavFruit = "Watermelon";
function writeMyFavoriteFruit($fruitName)
{
echo "My favorite fruit is: " . $fruitName;
}
writeMyFavoriteFruit($userFavFruit);
?>
The above code results in the following:
My favorite fruit is: Watermelon
Return Values
In the previous examples, our functions simply displayed a string to the user. Sometimes, you might not want your function to automatically display the result. Sometimes you might just want the function to return the result to you so that you can use it for your own programmatical purposes. That way, it's your choice whether you display it to the user, or do something else with the result.
To create a return value, you type return followed by the value you want to return.
Example
<?php
function sum($number1,$number2)
{
$total = $number1 + $number2;
return $total;
}
echo sum(100,4);
?>
The above code results in the following:
104