PHP 7 Fundamental Tutorial for Beginners – PHP Functions

What is PHP Functions

A function is a block of code written in a program to perform some specific task. We can relate functions in programs to employees in a office in real life for a better understanding of how functions work. Suppose the boss wants his employee to calculate the annual budget. So how will this process complete? The employee will take information about the statics from the boss, performs calculations and calculate the budget and shows the result to his boss. Functions works in a similar manner. They take informations as parameter, executes a block of statements or perform operations on this parameters and returns the result.

PHP Built-in Functions

  • A function is a self-contained block of code that performs a specific task.
  • PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype()print_r()var_dump, etc.
  • Please check out PHP reference section for a complete list of useful PHP built-in functions.

Why should we use functions?

  • Reusability: If we have a common code that we would like to use at various parts of a program, we can simply contain it within a function and call it whenever required. This reduces the time and effort of repetition of a single code. This can be done both within a program and also by importing the PHP file, containing the function, in some other program
  • Easier error detection: Since, our code is divided into functions, we can easily detect in which function, the error could lie and fix them fast and easily.
  • Easily maintained: As we have used functions in our program, so if anything or any line of code needs to be changed, we can easily change it inside the function and the change will be reflected everywhere, where the function is called. Hence, easy to maintain.

Creating and Invoking Functions

While creating a user defined function we need to keep few things in mind:

  1. Any name ending with an open and closed parenthesis is a function.
  2. A function name always begins with the keyword function.
  3. To call a function we just need to write its name followed by the parenthesis
  4. A function name cannot start with a number. It can start with an alphabet or underscore.
  5. A function name is not case-sensitive.

The basic syntax of creating a custom function can be give with:

function function_name(){
    executable code;
}

The declaration of a user-defined function start with the word function, followed by the name of the function you want to create followed by parentheses i.e. () and finally place your function’s code between curly brackets {}.

Example

Output: This is devopsschool.com

Function Parameters or Arguments

The information or variable, within the function’s parenthesis, are called parameters. These are used to hold the values executable during runtime. A user is free to take in as many parameters as he wants, separated with a comma(,) operator. These parameters are used to accept inputs during runtime. While passing the values like during a function call, they are called arguments. An argument is a value passed to a function and a parameter is used to hold those arguments. In common term, both parameter and argument mean the same. We need to keep in mind that for every parameter, we need to pass its corresponding argument.

Syntax:

function function_name($first_parameter, $second_parameter) {
    executable code;
}

Example:

Output: Sum of the two numbers 10 and 20 is : 30

Setting Default Values for a Function parameter

PHP allows us to set default argument values for function parameters. If we do not pass any argument for a parameter with default value then PHP will use the default set value for this parameter in the function call.

Example:

Output: Ram is 15 years old Adam is 12 years old

In the above example, the parameter $num has a default value 12, if we do not pass any value for this parameter in a function call then this default value 12 will be considered. Also the parameter $str has no default value , so it is compulsory.

Returning Values from Functions

Functions can also return values to the part of program from where it is called. The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns the value.

Example:

Output: The product is 30

Parameter passing to Functions

PHP allows us two ways in which an argument can be passed into a function:

  • Pass by Value: On passing arguments using pass by value, the value of the argument gets changed within a function, but the original value outside the function remains unchanged. That means a duplicate of the original value is passed as an argument.
  • Pass by Reference: On passing arguments as pass by reference, the original value is passed. Therefore, the original value gets altered. In pass by reference we actually pass the address of the value, where it is stored using ampersand sign(&).

Example:

Output: The original value is still 10 The original value changes to 20

Rajesh Kumar
Follow me