Introduction
PHP is composed of a set of one or more functions. Functions divide large programs into smaller, easier to handle pieces. Each function normally performs one specific task. Functions are used to reduce the code in the programming i.e. reusability. Functions can be placed anywhere in a page. PHP has more than 700 built-in functions.
Types of PHP Functions
In this article I will describe only three types of functions, which are as follows:
- Simple Functions
- Functions with Parameter
- Functions with Return values
Simple Functions
A Simple function does not take any arguments (parameters) or values. The function name should start with a letter or underscore (_), but not a number.
Syntax
function functionName()
{
message ;
}
Example
- <?php
- function CompnyName()
- {
- echo "MCN Solutions Pvt. Ltd. Noida.";
- }
- echo "My company name is ";
- CompnyName();
- ?>
In the above example we build a CompnyName() function and print a message inside this function. This function is called later for printing the message.
PHP Functions with Parameter
We can add arguments (parameters) to a function. Using parameters, we can add more functionally to a function. An argument is just like a variable of PHP. An argument (parameter) is specified inside the parenthesis of a function.
Syntax
function functionName (argument1,argument2)
{
message ;
}
Example 1
- <?php
- function CompnyName ($fullname) // one argument i.e. $fullname
- {
- echo $fullname;
- }
- echo "My company name is ";
- CompnyName ("MCN Solutions Pvt. Ltd. Noida.");
- ?>
In the above example we build a CompnyName() function and an argument as a $fullname. This argument shows the message when you write a message in this function.
Example 2
- <?php
- function sum ($X, $Y) // two argument i.e. $X and $Y
- {
- $Z=$X + $Y;
- echo "X=$X, Y=$Y, <br> Sum=$Z ";
- }
- sum(3,4);
- ?>
In the above example we build a sum() function and two arguments as $X and $Y. These arguments i.e. $X and $Y takes the values, when you the values in this function.
PHP Functions with Return values
In these types of functions, a function returns some values through the argument (parameter). Remember that a function should return at least one value.
Example 1
- <?php
- function sum ($X, $Y)
- {
- $Z=$X + $Y;
- return $Z;
- }
- echo "The Sum of 3 and 4 = ".sum(3,4);
- ?>
In the above example we build a sum() function and two arguments as $X and $Y. Now we declare a variable $Z, which takes a sum of $X and $Y then we will return $Z. Which shows the sum of X and y. In the above function we ed 3 and 4, which take by $X and $Y and returns the sum of these value to the variable $Z.
Example 2
- <?php
- function sum ($X, $Y)
- {
- $Z=$X + $Y;
- return $Z;
- }
- $X=5;
- $Y=7;
- $Sum=sum ($X,$Y);// assign the value of Z to $Sum
- echo "The Sum of 5 and 7 = ".$Sum;
- ?>
In the above example we build a sum() function and two arguments as $X and $Y. Now we declare a variable $Z, which takes to sum of $X and $Y then we will return $Z. Which shows the sum of X and y. In this example we assigned the value to $X and $Y i.e. 5 and 7 respectively. The sum of these values are returned by $Z to the $sum.
Conclusion
So in this article you saw types of functions and the use of functions in PHP. Using this article one can easily understand the concept of functions in PHP.
Some Useful Resources

Asfend YarPosted Feb 29, 2016, 10:58 AM
nice
Santhakumar MunuswamyPosted Aug 22, 2015, 7:16 AM
Good Article. Thanks for sharing
Sandeep KumarPosted Aug 21, 2015, 8:01 AM
thank you van lai :)
Sandeep KumarPosted Aug 21, 2015, 8:01 AM
thank you Gopi chand sir :)
Sandeep KumarPosted Aug 21, 2015, 8:00 AM
thank you Gowtham K sir :)
Sandeep KumarPosted Aug 21, 2015, 7:58 AM
thank you pankaj sir :)
Van LaiPosted Aug 20, 2015, 9:22 PM
I'm learning PHP from scratch. Thank you for sharing.
Gopi ChandPosted Aug 20, 2015, 1:42 PM
Its nice one
Gowtham KPosted Aug 20, 2015, 12:42 PM
Good one thanks for sharing
Pankaj Kumar ChoudharyPosted Aug 20, 2015, 12:16 PM
Nice Article Sandeep...............
Sandeep KumarPosted Aug 20, 2015, 11:15 AM
Thank you sir :)
Rahul PrajapatPosted Aug 20, 2015, 10:02 AM
Very helpfull article Sandeep Kumar sir