- PHP Functions
- PHP Built-in Functions
- PHP User Defined Functions
- Create a User Defined Function in PHP
- Syntax
- Example
- PHP Function Arguments
- Example
- Example
- PHP is a Loosely Typed Language
- Example
- Example
- PHP Default Argument Value
- Example
- PHP Functions — Returning values
- Example
- PHP Return Type Declarations
- Example
- Example
- Passing Arguments by Reference
- Example
- PHP Call Function
- Define and Call a Function in PHP
- How to Create a User-Defined Function?
- Examples to Implement PHP Call Function
- Example #1
- Example #2
- Example #3
- Conclusion
- Recommended Articles
PHP Functions
PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
PHP Built-in Functions
PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.
Please check out our PHP reference for a complete overview of the PHP built-in functions.
PHP User Defined Functions
Besides the built-in PHP functions, it is possible to create your own functions.
- A function is a block of statements that can be used repeatedly in a program.
- A function will not execute automatically when a page loads.
- A function will be executed by a call to the function.
Create a User Defined Function in PHP
A user-defined function declaration starts with the word function :
Syntax
Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.
Tip: Give the function a name that reflects what the function does!
In the example below, we create a function named «writeMsg()». The opening curly brace ( < ) indicates the beginning of the function code, and the closing curly brace ( >) indicates the end of the function. The function outputs «Hello world!». To call the function, just write its name followed by brackets ():
Example
writeMsg(); // call the function
?>
PHP Function Arguments
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:
Example
familyName(«Jani»);
familyName(«Hege»);
familyName(«Stale»);
familyName(«Kai Jim»);
familyName(«Borge»);
?>
The following example has a function with two arguments ($fname and $year):
Example
function familyName($fname, $year) echo «$fname Refsnes. Born in $year
«;
>
?php
familyName(«Hege», «1975»);
familyName(«Stale», «1978»);
familyName(«Kai Jim», «1983»);
?>
PHP is a Loosely Typed Language
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a «Fatal Error» if the data type mismatches.
In the following example we try to send both a number and a string to the function without using strict :
Example
function addNumbers(int $a, int $b) return $a + $b;
>
echo addNumbers(5, «5 days»);
// since strict is NOT enabled «5 days» is changed to int(5), and it will return 10
?>?php
To specify strict we need to set declare(strict_types=1); . This must be on the very first line of the PHP file.
In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:
Example
function addNumbers(int $a, int $b) return $a + $b;
>
echo addNumbers(5, «5 days»);
// since strict is enabled and «5 days» is not an integer, an error will be thrown
?>
The strict declaration forces things to be used in the intended way.
PHP Default Argument Value
The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
Example
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
PHP Functions — Returning values
To let a function return a value, use the return statement:
Example
PHP Return Type Declarations
PHP 7 also supports Type Declarations for the return statement. Like with the type declaration for function arguments, by enabling the strict requirement, it will throw a «Fatal Error» on a type mismatch.
To declare a type for the function return, add a colon ( : ) and the type right before the opening curly ( < )bracket when declaring the function.
In the following example we specify the return type for the function:
Example
You can specify a different return type, than the argument types, but make sure the return is the correct type:
Example
Passing Arguments by Reference
In PHP, arguments are usually passed by value, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed.
When a function argument is passed by reference, changes to the argument also change the variable that was passed in. To turn a function argument into a reference, the & operator is used:
Example
Use a pass-by-reference argument to update a variable:
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.
Web development, programming languages, Software testing & others
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.
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.
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.
Employee("Akash"); Employee("Prakash"); ?>
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.
Employee("Lakshmi","780540"); Employee("Rohit","780541"); Employee("Jenny","780542"); ?>
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.
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 –
25+ Hours of HD Videos
5 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
92+ Hours of HD Videos
22 Courses
2 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
83+ Hours of HD Videos
16 Courses
1 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
PHP Course Bundle — 8 Courses in 1 | 3 Mock Tests
43+ Hours of HD Videos
8 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5