Updated March 23, 2023
Introduction to PHP Call Function
In PHP, the functions are of two types i.e. built-in and user-defined functions. There are a lot of built-in functions that can be called directly from the program by the programmers or the developers. These built-in functions have a specific meaning for the task to be performed. A user-defined function is specifically developed by the program that contains the code to be performed with respect to the application. The developer writes a set of code that has to perform the task based on the requirement in the application.
Define and Call a Function in PHP
There are two types of functions in PHP i.e. Built-in and User-defined functions. These functions are used in the program to perform various tasks. In PHP, a huge number of functions are defined and can be used anywhere in the program. Though it has a lot of advantages of not writing the code again and again and the functions come with the package itself, it is easy for the developer to use the built-in functions. But all the built-in functions have a predefined meaning and perform a specific task itself. So the developer started developing user-defined functions where the developer can write the specific task code inside any function and can be used anywhere in the program. The user-defined functions are called in the program in a class or in a method of a program to perform the task based on applications requirement.
How to Create a User-Defined Function?
In PHP, a user-defined function has to be declared with the keyword “function”. If we declare the function name and the code to be executed has been written inside it then it can be called anywhere in the program.
Syntax:
function function_name()
{
//statements
}
In the above syntax, the function_name is the name of the function that is to be called in the program and function is the keyword that is used to declare the function. In PHP, a function is declared with the function keyword prefixed with the function name and the calling of a function in a program is done by just calling the name of the function wherever required.
Examples to Implement PHP Call Function
Let’s take examples to understand the concept of calling a function in PHP.
Example #1
In the below example, the function Write_Output is the name of the function and the function is called in the program with the same name below. This line will call the defined function and executes the statements written inside the function and will exit once the last statement of the code gets executed.
Code:
<?php
function Write_Output()
{
echo "This is the sample example of a function!";
}
Write_Output();
?>
Output:
Example #2
1. In PHP, a function can be a parameterized one i.e. passing arguments to the function. An arguments are just like the variables we define in the program. We can simply pass the arguments after the name of the function inside the parenthesis and can be added as much as we want by adding a comma in between the arguments. These arguments will be mapped while calling the function. It will throw error if you are not passing the correct number of arguments when calling the function.
Code:
<?php
function Employee($ename) {
echo "$ename Patil \n";
}
Employee("Akash");
Employee("Prakash");
?>
Output:
In the above example, Employee is the name of the function and ename is the argument passed to the function in the declaration. The Employee function is called below the function declaration and the values are passed to the function while calling. The output of the program will be the names of the employee passed in the function while calling it.
2. In the below example, the employee names are printed and also the employee ID is printed along with it. When an organization has chunks of data to be stored in the database, we can simply write this kind of code to handle the data with ease.
Code:
<?php
function Employee($ename, $id) {
echo "Employee name is $ename and Employee id is $id \n";
}
Employee("Lakshmi","780540");
Employee("Rohit","780541");
Employee("Jenny","780542");
?>
Output:
In PHP, the variables are not strictly based on the datatypes depending upon its value or data. The datatypes are loosely coupled and do not follow any strict rules. So we can add, subtract and do multiple operations on variables that have different data types as well.
Example #3
As we have seen all the examples, we can clearly see that the user-defined functions are more beneficial to the developer as it helps a lot from an application perspective and also is used to get the desired output. The goal of using functions in a particular program is that it can create his own code and develop the application based on requirements.
Code:
<?php
function mulNumbers(int $x, int $y)
{
return $x * $y;
}
echo mulNumbers(5, 13);
?>
Output:
As we have seen in the above example that the integer can be multiplied with a string and a string can be used along with float etc. So PHP is a loosely typed language. In the above example, we could clearly see that the variable x and y are declared as integer and while calling they just use one variable as integer and another one as a string and the desired output also gets fetched.
Conclusion
In this article, we discussed how to call functions in PHP i.e. user-defined function. Then we discussed different forms of user-defined functions and syntax with example. The functions can be written for a specific application and to perform a specific task i.e. calculation of payroll, adding new entries, etc. So the developer can easily modify the code for the flexibility and can call it anywhere in the program.
Recommended Articles
This is a guide to PHP Call Function. Here we discuss how to define and how to create user-defined PHP Call Function along with examples and code implementation. You can also go through our other suggested articles to learn more –