Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves the power of PHP language. Besides built-in function, PHP has capability to declare user defined function like other programming language. So, PHP becomes a dominating programming language having a number of built-in functions and user defined function capability.
A newbie PHP programmer should have enough skill on PHP functions along with other PHP basic terms because PHP program frequently uses built-in function as well as user defined function. In my previous article, I explained PHP loop statements elaborately and in this article I am going to discuss PHP function’s anatomy elaborately.
PHP Function
A function is a block of statement that takes one or more input values in form of argument and does some processing and then returns a value. PHP function can be divided into the following two categories.
- Built-in function
- User defined function
PHP Built-in Function
Built-in functions are those functions whose definition is predefined in PHP. More than thousands of built-in functions are present in PHP language. Built-in function has predefined arguments and return that we have to know before usage.
If you do practice regularly, you will able to use more and more built-in functions in your PHP program. So, do Google and find your required function or view PHP documentation and then apply that function in your application. Some popular built-in functions used frequently in PHP program are echo(), printf(), isset(), strcmp(), strlen(), strncmp(), strrev(), is_array(), array_merge(), array_push() etc.
PHP User Defined Function
User defined function is that function whose definition is defined by the PHP programmer. User defined function does not execute immediately when a PHP page loads. A function is executed when the function is called from anywher of the program.
A user defined function has the following two part.
- Function definition
- Function calling
PHP User Defined Function Definition
To create a function in PHP language, we have to first declare the function which is known as function definition. A User defined function is declared like the following structure.
return return_value;
}
From the above structure we can see that there are five parts of a user defined function.
- function keyword (required).
- Function name (required).
- Function body enclosed with curly braces (required).
- Function arguments within parentheses (optional).
- Function return value (optional).
PHP user defined function must start with function keyword and a function name separated by a space. Required arguments can be passed through argument part. The function body must start with starting curly brace ({) and end with ending curly brace (}). Optionally, a user defined function may have a return value with return keyword. Return part of a user defined function returns the final result to the caller function.
PHP Function Definition Example
The following program is a user defined function that will add two numeric values and return the result to the caller function.
<?php
function add($a,$b){
$c=$a+$b;
return $c;
}
?>
The above add() function will not be executed immediately when the PHP page loads. It will be executed after calling the function from anywhere in this page.
PHP Function Calling
After creating a user defined a function, the function must call to get the estimated result. PHP user defined function is called like the following structure.
From the above structure we can see that to call a user defined function we have to mention the function name with parentheses and if any argument of the function is present, the argument will be mentioned within parentheses.
PHP Function Calling Example
In the previous example, we have created a user defined function (named add()) that will add two numeric values and return the result to the caller function. We will now call the add() function to get the function return value.
The following program is an example of PHP user defined function definition and calling and to get the return value.
<?php
function add($a,$b){
$c=$a+$b;
return $c;
}
$result=add(5,6);
echo "The result is ".$result;
?>
Output
In the above program, the add() function is being called with the required arguments. After processing the operation, the function is returning the result which is being stored in the $result variable and then the stored value is being showed with the PHP built-in function echo.
If any function does not have any argument and return value, the function is declared like the following example.
<?php
function show(){
echo "I am called without arguments and I have no return value.";
}
show();
?>
Output
Here, the show() function is a user defined function which has no argument. So, no argument has supplied at function calling. The show() function also has no return value. So, the return keyword has been ignored here.
PHP function definition and function calling has been explained in this article. I hope you will now be able to define and call a user defined function in PHP program. However, if you face any confusion, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.