PHP has functions for opening and reading files, validating data, and performing other actions. The language consists of a variety of in-built methods that are available to use.
It is also possible to generate unique PHP functions. By declaring user-defined functions that you will repeatedly use in your code, you will save time and raise productivity.
Contents
PHP Function: Main Tips
- There are over 1 000 PHP functions in total. You are free to create new methods as well.
- Functions execute after they are invoked by writing their name along with values you wish to assign. These values are written between parentheses next to the function name.
Inbuilt Functions
PHP comes with a list of predefined functions. It is crucial to understand the way PHP functions are defined, how they handle assigned values and what they return.
Methods can return different results depending on whether the function failed or executed the process successfully.
We divide internal PHP functions according to their purposes: string functions, array functions, functions for error-handling, directory functions, libxml functions and miscellaneous functions.
User-Defined Functions
To define your own PHP functions, you have to follow this syntax:
function functionName() {//what will the function do;}
<?php
function write_message() {
echo "BitDegree Learn!";
}
write_message(); // calls the function
?>
The example above shows how to define the write_message()
function. By defining the function PHP code will have to execute, we set it to output text saying BitDegree Learn! after being called.
For the user-defined function to work, include all of this information:
- Use the
function
keyword to indicate a new method. - Define the name of the function. Make sure it represents its purpose.
- Include parameters of the function in parentheses. Multiple parameters should be separated by commas.
- Curly brackets include statements that define the way the function works.
Note: all valid PHP function names start with either an underscore (_) or a letter. They are not case-sensitive.
PHP Function Arguments
You can pass data to a function by using arguments. They will act very similarly to variables.
Note: you declare arguments between the parentheses (). To enter multiple arguments, you have to separate them by commas. There is no limit to how many you may enter.
In the code example below, we pass an argument called $name
. After calling the function, the value we enter between the parentheses is assigned to the argument variable, called $name
:
<?php
function getFamilyName($name) {
echo "$name Jones.<br>";
}
getFamilyName('Jani');
getFamilyName('Hege');
getFamilyName('Stale');
getFamilyName('Kai Jim');
getFamilyName('Borge');
?>
Now, in this example we define a function that gets two arguments passed to it ($name
and $birthyear
):
<?php
function getFamilyName($name, $birthYear) {
echo "$name Refsnes. Born in $birthYear <br>";
}
getFamilyName('Hege', 1975);
getFamilyName('Stale', 1978);
getFamilyName('Kai Jim', 1983);
?>
Default Argument Value
The code example below looks into giving arguments default values. Once we call the set_height()
function without specific arguments, it will take the value we assigned for the argument when defining the PHP function:
<?php
function setHeight($minHeight = 10) {
echo "The height is : $minHeight <br>";
}
setHeight(350);
setHeight(); // using the default value of 10
setHeight(135);
setHeight(80);
?>
Return Values
Finally, this code example uses the return
statement to make the function in PHP return a value after successful execution.
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
Using Variables as Function Names
In PHP, it is possible to use variable functions. It means that when variables have parentheses, PHP will try to find a method with the same name and execute it. Variable functions are useful for completing callbacks and function tables.
<?php
$question = "What is the magic word?<br>";
if (isset($question)) {
echo $question;
echo "Please!";
} else {
echo "If you don't have a question you don't get an answer.";
}
?>
The code example above uses isset() as a variable function and checks whether a question is asked. Remove the second line to get a different result.
Note: you cannot use variable functions with language constructs such as echo and print. To use them as variable functions, you should apply wrapper methods.
PHP Function: Summary
- You can use more than a thousand functions that are inbuilt in PHP and ready to call. For convenience, you can even generate user-defined functions as well.
- To execute a particular function, you need to call it (type its name).
- Parentheses next to the function are used to contain assigned parameters.