PHP 8 Functions

PHP functions are like other programming languages. There are a large number of in-built functions in PHP, and in addition you can create your own custom 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.

PHP function definition syntax

Function definition has 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 - The function defined by users.
  • Built-in function - The predefined function call by users.


User Defined functions

The general syntax of defining a function in PHP is -

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

A function is defined using a keyword function, 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 parameters or no parameters in a function according to the requirement. Function statement start and stop with curly braces. The statement inside a function is processed some operations on the parameters and return the 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 the function and calling the function should be same.





Scope of the function

The variable defined outside of the 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

The 'return' statement is used to get function value or end 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

PHP built-in function call

PHP has a 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 the substring of a string, function to execute all types of MySQL queries, functions to handle a string like to convert a string in uppercase or lowercase, or to get the 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