Types of Functions in PHP

Functions in PHP

function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. A function is block of code written in a program to perform some specific task. It has the following syntax:

 

function functionname($param1, $param2) {    
// Code to be executed  
  return $variable; 
}

 

Types of Functions in PHP

There are major two types of functions in PHP:

1. Built-in Function: PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

Example:

<?php
 function addNumbers($x, $y) {
    return $x + $y;
}
echo addNumbers(5, 7);
?>

Output:

12

2. User Defined Function: We can declare and call user-defined functions easily. PHP allows our own customized function called the user-defined function.

Example:

<?php
  $str = "Welcome to Webeduclick!";
  echo strlen($str); 
?>

Output:

23