Create function in php with example

In web development, It's a 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 thousand in-built function provided by PHP like string function, array function 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 function with 4 types in PHP:

  1. Function without arguments and return value
  2. Function with arguments
  3. Function with return values
  4. Function with arguments and return values

Syntax :

PHP allows user to define their own function. Below is syntax for define function :

Function declaration starts with function keyword. Another thing is function name which need to be unique into program and it can't be reserve keyword or in-built function name like split or in_array. Arguments and return type are optional. Let's take examples for all types of functions.

Function Without Arguments and Return Value

Here, we define function which just prints message whenever it's called.

In above example, we have defined welcome function and called it. This will simply print welcome message.

Functions With Arguments

PHP function can have arguments or parameter. Those parameters plays role into function's code. Let's take example where user pass two parameter and function will print addition of those parameters.

In this example, we have defined 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 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 in below example for default argument value :

Using sum function first time it will take 10 and 20 as default value while calling second time it 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 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 stand of printing it. so we can use that message into further code.

Functions With Arguments and Return Value

This type is combination of function with argument and function with return value. Which means we can pass parameter and return value too. Let's take example for it.

Same as previous sum function it will take two values and perform addition but in 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.


Share your thoughts

Ask anything about this examples