Lesson 10 – PHP Functions

Almost all the programming languages use functions and PHP is no different. A function is just another piece of code that takes one or more parameters as input and returns the value after processing everything. There are, in fact, two types of functions in PHP, the built-in functions and the custom functions.

It is worth mentioning that you hardly need to create a custom function while developing in PHP. It has more than a thousand such functions that help you perform any type of task you can imagine. You can find the complete list of PHP Built-in Functions here.

However, creating a custom function in PHP is also a breeze. All you need to do is to start the name of the function with the keyword “function” followed by the actual name. Subsequently, you will put all the code in the curly brackets. Then you also have to call the function by writing its name outside the curly brackets. Here is an example. 


function addSomething() {
    echo 2 + 4; 



We have created a function named “addSomething” and then performed an arithmetic operation inside the brackets. Then we have called the function by writing the name of our function outside the brackets. This is the basic syntax of a custom function in PHP.

The above code will display the following result.

PHP also allows you to pass as many parameters as you like inside a function. These parameters work similarly to variables. In the following example, we are going to print the sum of two integer parameters by passing them inside a function. The point to note here is that you will pass the actual value of parameters in the function at the time of calling it. Here is an example.


function addSomething($number1, $number2) {
    $number = $number1 + $number2;
    echo "The sum of two integer parameters is $number";

addSomething(20, 30);


Here is the output of the above piece of code.

With this, you have completed your basic training in PHP. Now, it is high time you concentrate on learning advanced PHP skills by taking advanced and more detailed courses.