PHP 7 Functions

A function is a way of wrapping up a piece of code and giving that chunk a name, so that you can utilize that piece later in just one line of code. Functions are most valuable when you will utilize the code in more than one place, but they can be helpful even in one-use situations, because they can make your code much more readable.

Function definition syntax

Function definition have the following form:

function function_name(parameter1, parameter2, ....)
{
	statement1;
	statement2;
	...
}

That is, function definitions have four parts -

  • The special word function
  • The name that you want to give your function
  • The function's parameter list - dollar-sign variables separated by commas.
  • The function body - a brace-enclosed set of statements.

The name of the function must be comprised of letters, numbers, and underscores, and it must not begin with a number. Unlike variable names, function names are converted to lowercase before they are stored internally by PHP, so a function is the same regardless of capitalization.





Types of functions in PHP.

  • User defined function - functions defined by users.
  • Built in function - predefined functions call by users.

User Defined functions

The general syntax of defining a function is -

<?php
    function function_name(parameter1, parameter2) {
        statement ;
    }
?>

A function is defined using function keyword, and the function name should be unique, If a function with the same function name is defined twice then it will return an error. We can pass one or more parameter or no parameter in a function according to the requirement. Function statement start and stop with curly braces. The statement inside a function are processed some operations on the parameters and return processing result. A function statement ends with a return statement.

Example -

<?php
    function addition($var1, $var2){ 
        $sum = $var1 + $var2; 
        return $sum; 
    }
    $sumresult = addition(4, 10);
    echo $sumresult; // 14
?>

Output -

14

We can call this addition function anywhere in the code, where there is need to sum two values.
The number of parameters in defining function and call function should be same.





Scope of the function

The variable defined outside of function cannot be used inside the function, to make use of this variable inside the function, we will have to pass this as a parameter. Similarly, the variable defined inside the function cannot be called outside.

Like in the below function

Example -

<?php
    $var = 54; 
    function demo() 
    { 
     $var = 10; 
    } 
    echo $var; 
?>

Output -

54

Returing function values

'return' statement is used to get function value or ends the execution of a function. The return value is of some datatype. It can be any integer, float, string, array.

Example -

<?php
function square($a)
{
    return $a * $a;
}
echo square(6);  
?>

Output -

36

Built In function call

PHP has rich set of built in functions that make our development so easy. The PHP functions cover all small to small and large to large tasks manipulations. Like to get substring of a string, function to execute all types of mysql queries, functions to handle string like to convert a string in uppercase or lowercase, or to get substring of a string, functions to perform on arrays and also functions used in some advanced applications like, music manipulations, credit card processing, file compression etc.
Some useful php built-in functions are listed here







Read more articles


General Knowledge



Learn Popular Language