In web development, It's considered best practice to split code into small functions which can be reused in further applications per requirement. Before starting let's understand more about functions.
There are more than a thousand in-built functions provided by PHP like string functions, array functions, and more.
What is Function?
A function is a block of statements that can be used repeatedly in a program. It is executed only when called. It can have arguments and return values as per requirements.
We can create functions with 4 types in PHP:
- Function without arguments and return value
- Function with arguments
- Function with return values
- Function with arguments and return values
Syntax :
PHP allows user to define their function. Below is a syntax for defining function :
Function declaration starts with function keyword. Another thing is function name which needs to be unique to program and it can't be a reserved keyword or an in-built function name like split or in_array. Arguments and return types are optional. Let's take examples for all types of functions.
Function Without Arguments and Return Value
Here, we define a function that just prints a message whenever it's called.
In the above example, we have defined welcome function and called it. This will simply print a welcome message.
Functions With Arguments
PHP function can have arguments or parameters. Those parameters play a role in function's code. Let's take an example where user passes two parameter and function will print addition of those parameters.
In this example, we have defined the sum() function. Which will take 2 arguments and print sum of those parameters. Here, we have called sum function two times so it will print 2 values.
We can also set a default value for those parameters. In case, while calling function user doesn't pass arguments then it will use default value and execute code. Have a look at below example for default argument value :
Using sum function first time will take 10 and 20 as the default value while calling second time will take parameter's value to perform addition.
Functions With Return Value
The concept of return value is used when we need to get output of function into the main function or code where we called function. i.e. we want to define function to get some values like welcome message.
Here, we have defined same function as first example but this function will return welcome message in a stand of printing it. so we can use that message in further code.
Functions With Arguments and Return Value
This type is a combination of function with argument and function with return value. Which means we can pass parameters and return values too. Let's take an example of it.
Same as the previous sum function it will take two values and perform addition but in a stand of printing it will return new value to its initiator and we can use that value in further operations. This function type is generally used in actual programming. Here, we just perform logic based on parameters and return output.
Conclusion
In this article, we have learned about functions in PHP and its types with examples. While web development, functions are very useful to reuse code and save time. So try to use functions as much as you can.