Php – Functions – POFTUT

Php – Functions

[rps-include post=6522]

Up to now we have write simple and little codes but a real applications can not be accomplished with this type of code. We need to write a lot of code and use some part of the code again and again without same copies. Functions gives us the ability to use given part of code to run multiple time and from different parts of the applications. Functions also provides some abstraction so we only provide required parameters and get the result we do not need to now the implementation details.

Define Function

Functions have body where the function code is implemented. Function is defined with function keyword. After that the name of the function and the parameters are provided inside brackets. And the last and the biggest part is the function body in curly brackets. Here is syntax.

function NAME ( PARAMETERS ){



In the example we will create a function name message which have no parameter and only prints some text. Parameters in function definition are optional so if we do not need them just () is enough. We will call our function with its name and parameters but there is no parameter.

function message(){

    echo "This is a message from";



Pass Parameters

Previous example we do not provided parameter to the function or defined it.  Most functions generally requires parameters to complete given job. We can define parameters after function name in brackets () like a normal variable name.

In this example we will define two variables named $a and $b . The function name will be sum because we will sum the given variables and print the result inside function.

function sum($a , $b){

   echo $a+ $b;


sum(2,3); //Print 5

Return Value

In previous example we have completed related operations inside function. But the general usage is a bit more different. Functions generally used to return some data or result. We will use return keyword in order to return result from the function.

LEARN MORE  Python Class Tutorial with Examples

The example is the same with previous one but we have returned result and printed outside of the function which makes things more clear and easy to understand.

function sum($a , $b){

   return $a+ $b;


echo sum(2,3); //Print 5

Pass Parameters By Reference

Parameters normally passed by copying their values into given function parameter variables and any change in the function parameter variables do not affects the provided parameters. In some situations we may want to change provided parameters in the function. For this we should provide parameters by reference. We will use & in the function definition before parameter names.

In the example we will provide variables to the function named increase as reference.

$x = 5;

function increase(&$a){




echo $x;

This will print 6 because $x is passed as reference to the $a parameter and the $a parameter is increased one.

Default Parameter Values

While providing parameters to function some parameters may get generally the same value. While calling this function multiple times things will be trivial task. We can set parameters default value if they are not explicitly defined.

In this example we set the function sum parameter $b as 5 as default but if we want we can specify explicitly like normal function call.

function sum($a , $b=5){

   return $a+ $b;


echo sum(2); //Print 7

Dynamic Function Call

Dynamic function call is an interesting feature about Php functions. We can call a function by specifying its name as string value.

In this example we will call the function sum with a string variable named $functionname .

function sum($a , $b){

   echo $a+ $b;



$functionname(2,3); //Prints 5

Recursive Functions

Some original problems need more original and efficient solutions. Recursive functions used to dive parse a big problem into little parts just calling itself again and again. Here is an recursive example

function fact($n) { 
  if ($n === 0) { // our base case 
     return 1; 
  else { 
     return $n * fact($n-1); // <--calling itself. 

[rps-include post=6522]

Leave a Comment