What is functions in PHP | How to use functions in PHP | Syntax of functions

array_replace in PHP | array_replace_recursive in PHP | Advanced Array in PHP

What is a PHP function?

Functions in PHP are a set of blocks of statements that are run multiple times in a code. Functions are used in place of those blocks of statements that are running again and again in a code. So, if you have some statements to run at different time

For example:

Echo “hello PHP”; 
Echo “<br>”; 
Echo “my world!”;

Suppose, the above code needs at multiple places in a code. So instead of writing these statements, again and again, we will create a function that will be used whenever it is required.

For example:

ABC() { 
Echo “hello PHP”; 
Echo “<br>”; 
Echo “my world!”; 
}

Now, the user will use ABC() whenever he needs to print those statements. This saves a lot of time for a user by not writing the same code again and again and the code will look much pleasing to the eyes of the reader.

Function Definition: In simple words, function definition means telling a function what task needs to be performed at the time of its execution.

Syntax of the function definition:

Function functionName() { 
Statements // could be anything like if, echo, etc. 
}

Function calling: Function calling means we have to call the function at a place where we want its execution. if you want to run the statement mentioned in function definition like echo, if or anything else, then this function must be called somewhere to execute its command. Without function calling a function cannot run its task.

A function is called by the following syntax:

functionName();

Example program to write PHP function:

The output of the code:

hello PHP

Explanation of the above program:

Definition of function is function MyPhp() i.e. before curly braces. Calling of function is after curly braces i.e. MyPhp();

Types of functions:

PHP supports two types of functions, one is built-in functions, and another is user-defined functions.

Built-in functions– built-in functions are the functions that are predefined by PHP. There are many built-in functions like- array, date, time, calendar, etc.

User-defined functions– these functions are not predefined. They are named by the user itself. Users can name any function accordingly. E.g. hello(), myFunction(), etc.

Rules to define a user-defined function name:

  • Functions should not contain any spaces.
  • You can use underscore_, camelcase e.g. (functionName) or hyphen(-)
  • The function name should not start with a number. It can be inserted inside a function name. e.g. function99php

Advantages of PHP functions:

  • Saves time– the user will not need to write the same code number of times. It saves a lot of time for a user.
  • Changes can be done easily– the user can simply change its code written in function at one place. Without functions, the code will need to be changed at all places wherever it is written.

PHP Functions with Parameters

Each function is defined in () round braces. They can be used with some parameters. Function parameters are defined inside () braces. E.g hello($num1, $num2);

$num1 and $num2 are the variables that we will use at the time of function definition. These defined variables are known as function parameters.

PHP Functions with Arguments:

Arguments are the values that are defined for their variables. For example, variable $num1 has the value “hello”. Here “hello” is an argument. They are used at the time of function calling.

Each function at the time of calling needs arguments.

Check the below example:

Output:

hello

Functions with multiple parameters:

Output:

Hello world

A function can be called at different times in a code using different values each time. Check the below example:

Output:

Hello 
world 
30 30.256

Using variables in function calling:

Output:

Hello 
world 60

PHP functions without arguments:

We can use PHP functions without arguments by defining their default argument value taking arguments empty.

Check the below example:

Output

Hello 
world 64

PHP functions with return values:

In PHP, return statements are used to return some values by the function when the function is executed. Here echo statements are printed outside the syntax of the function definition.

Method I

<?php 
function MyPhp($num1, $num2)
{
    return $num1 + $num2;
}
echo MyPhp(10, 5); 
?>

Method II

<?php 
function MyPhp($num1, $num2)
{
    $sum = $num1 + $num2;
    return $sum;
}
echo MyPhp(10, 5); 
?>

Method III

<?php 
function MyPhp($num1, $num2)
{
    $j = $num1 + $num2;
    return $j;
}
$var = MyPhp(10, 5);
echo $var; 
?>

What is functions in PHP | How to use functions in PHP | Syntax of functions
Show Buttons
Hide Buttons