PHP Functions
A piece of code that can be reused many time is called function.
function functionname(){
//code to be executed
}
There is two type of function in PHP:
- Built in function
- User defined function
PHP Built-in Functions
PHP has a huge collection built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype(), print_r(), var_dump, etc.
PHP User-defined Functions
Apart from the built-in functions, PHP allows us to create our own customised functions called the user-defined functions. Using this we can create our own packages of code and use it wherever necessary by simply calling it.
Creating a Function
While creating a user defined function we need to keep few things in mind:
- Any name ending with an open and closed parenthesis is a function.
- A function name always begins with the keyword function.
- To call a function we just need to write its name followed by the parenthesis
- A function name cannot start with a number. It can start with an alphabet or underscore.
- A function name is not case-sensitive.
Advantage of PHP Functions
- Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.
- Less Code: It saves a lot of code because you don't need to write the logic many times. By the use of function, you can write the logic only once and reuse it.
- Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.
- Easy to find error: We can easily find the error if we use function because it is separate from php programming.By find where the function call we get the function name and easily solve the error.
PHP Function Arguments
The information or variable, within the function's parenthesis, are called parameters. These are used to hold the values executable during runtime. A user is free to take in as many parameters as he wants, separated with a comma(,) operator.
These parameters are used to accept inputs during runtime. While passing the values like during a function call, they are called arguments. An argument is a value passed to a function and a parameter is used to hold those arguments. In common term, both parameter and argument mean the same. We need to keep in mind that for every parameter, we need to pass its corresponding argument.
function function_name($first_parameter, $second_parameter)
{
executable code;
}
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list.
PHP Call By Reference
By default, value passed to the function doesn't modify the actual value (call by value). But we can do so by passing value as a reference.
By default, value passed to the function is call by value. To pass value as a reference, you need to use ampersand (&) symbol before the argument name.
PHP Function: Default Argument Value
User can specify a default argument value in function. While calling PHP function if don't specify any argument, it will take the default argument.
File: functiondefaultarg.php
PHP Function: Returning Value
PHP Call By Value
PHP allows to call function by value and reference both. In case of call by value, if it is modified inside the function the actual value is not modified.
Functions with Parameters
The parameters work like placeholder variables within a function; they're replaced at run time by the values provided to the function at the time of invocation.
function myFunc($oneParameter, $anotherParameter){
// Code to be executed
}