PHP Functions

A function is a block of code designed to perform a particular task. It contains organized and reusable code that will be executed by an event or a call to that function. By the using of functions we do not need to write the same code multiple times in order to achieve our goals.

PHP has a huge collection of internal or built-in functions that the user can call directly within the PHP scripts to perform a specific task, for example we are previously used var_dump() built-in function. In addition to the built-in functions, PHP also allows us to define our own functions. It is a way to create reusable code packages that perform specific tasks and can be kept and maintained separately form main program.

The most common way to define a function in PHP is by using the keyword function, followed by a unique function name, and a list of parameters or arguments (that might be empty if there's not any parameter), and a statement block surrounded by curly braces. The Syntax is :
 function function_name(parameter1, parameter2, parameter3) {
   code to be executed
 }
And the function without any parameter
 function function_name() {
   code to be executed
 }
The function name can contains letters, digits, underscores, and dollar signs, but remember that the name of function would always start with letters or underscore and nothing else. Function parameters are listed inside the parentheses () in the function definition. The parameters are the values received by the function when it is called or invoked and inside the function, parameters behave as local variables. A function will not execute immediately when a page loads, instead it will be executed by a call to the function. The basic example of function is as follows :
<?php

  function message() {
    echo "Hello world!!";
  }

?>
The above code define a function called message(), which simply display "Hello world!!" on the page. And also note that it takes no parameters.

Invoking or calling a Function :

A function is not executed before it is invoked or called. To invoke a function we simply need to write the name of that function with parameters inside the brackets, and if it does not have parameters then simply with empty brackets. The basic syntax for calling a function with parameters :
 myfunction(parameter1, parameter2, ....pamareter_n);
Calling a function without arguments
 myfunction();
Now to call the message() function, the code will be :
<?php

  function message() {
    echo "Hello world!!";
  }

  message();

?>
Output :

Hello world!!

Calling function with parameters :
<?php

  function message($name) {
    echo "Hello $name<br/>";
  }

  message("John");
  message("Luci");
  message("Mary");
  message("Sam");
?>
Output :

Hello John
Hello Luci
Hello Mary
Hello Sam

Function Return Statement :

The PHP function can have an optional return statement. The return statement specifies the value which will be returned to where the function was called from. For example the below function return the sum of two number.
 function add_num($n1, $n2) {
   $num = $n1 + $n2;
   return $num;
 }
Now when we call the above function with two parameter, then it will return the sum of two numbers.
 add_num(10, 20);
Example :
<?php

  function add_num($n1, $n2) {
    $num = $n1 + $n2;
    return $num;
  }

  echo add_num(10, 20);
  echo "<br/>";
  echo add_num(25, 35);
  echo "<br/>";
  echo add_num(32, 18);
  echo "<br/>";
?>
Output :

30
60
50

Function with Default argument value :

In php we can specify default argument values in function. The default argument value is the value of function's argument (or parameter), which is applicable when we do not provide any argument value at the time of calling that function. Lets see an example :

Example 1 :
<?php

  function add_num($n1=10, $n2=10) {
    $num = $n1 + $n2;
    return $num;
  }

  echo add_num();
  echo "<br/>";
  echo add_num(10, 20);
  echo "<br/>";
?>
Output :

20
30

At the above example the first call to add_num() function is without any argument value, so it  uses the default argument values and return the results.

Example 2 :
<?php

  function add_num($n1=10, $n2=10) {
    $num = $n1 + $n2;
    return $num;
  }

  echo add_num(30);
  echo "<br/>";
  echo add_num(10, 20);
  echo "<br/>";
?>
Output :

40
30

Now in the above example we only provide single argument at function call so it takes the default value for second argument only.


Next Topic :