How to Create the Function in PHP?

Last updated on August 31, 2023 by Ayaz Ali Shah

In PHP, to create the function we use the function keyword along with the function’s name. It can be any name that should be before the parenthesis (). A function name always starts with the function keyword.

PHP functions are not case-sensitive. Also, it allows us to pass parameters containing different types of data. You also can use the return in function for printing.


function functionName(){
   Code that has to execute...

So how do we call the function?

To call a function, just write its name followed by parentheses ().


Look at the below example for creating and executing the function.

function sayHello(){
   return "Hello world";

Below we are executing the function.

echo sayHello();

Hello world

Example 1:

function getTotal($param1, $param2){
	return $param1+ $param2;

echo getTotal(30,50);
//output: 80

In the above example, we create the function whose name is getTotal() that takes two parameters and returns the sum of two numbers.


This article demonstrates the user-defined function in PHP. It covers the function’s creation, execution, and parameters.

Share on:

Related Posts

Tags: PHP,