PHP: Function And Why We Use It In Web Application?
Table of Contents
PHP functions are the same as a C programming language. A PHP function is a part of code that is reused many times for some tasks. It takes an argument as an input and returns the value. PHP programming has many more built-in functions, and we can create our custom functions (user-defined functions) in PHP.
We can relate the use of functions in programs with the teachers in a school in real life for a better understanding of how PHP functions work.
Let us assume that the headmaster wants his teachers to calculate the number of students. So, how this process is done?
The teachers will take information about the statistics from the headmaster. They then perform the required calculations to find the total number of students and show the result.
PHP Built-in Functions
PHP provides us with 100s of built-in functions, and it already has a code in PHP and is stored in the PHP functions. If you want to use these functions, we are just required to call the function, as per our need.
For example in built-in function: var_dump, fopen(), print_r(), gettype() and many more functions.
PHP User Created Functions
In PHP, we can create our personal customized or user-created functions, which are called User Created functions.
Some rules are used to create a custom function in PHP. They are as follows:
- In PHP Function the names should start with a letter or an underscore, but not with a number.
- In PHP, the function name must be unique. So we cannot define the same function name more than once.
- The function name must not contain spaces. If we want to add space between function names then use underscore (_).
- PHP Functions can optionally accept parameters and return values too.
Create a User Defined Function in PHP
user-defined function in PHP declaration starts with the word ‘Function’ keyword.
the user-created function Syntax: –
function functionName(){
//write your code here
}
Example:
Output: Hello world.
In the above example, you can create a function name like “hello()”. The starting curly bracket (‘{‘) indicated the start of the function, and the closing curly bracket (}) indicated the ending of the function. The function output is “Hello world!”. You can just call the function name like hello().
PHP Function with a Arguments
In PHP messages are passed to the functions with an argument. In PHP function parameter is just like a PHP variable. The parameters are specified after the function name declaration and inside the parentheses. So, we can add many parameters, and we are just required to separate them with commas.
Example:
Output: Smith Born in 2001.
In the above example, we define two arguments ($fname and $year) in a function. When the student () function is called we also pass a name and year (i.e., Smith 2001) along with it, and the name and year are used inside the function, which gave the outputs name and year.
PHP Function With Default Argument Value
In PHP function allows us to set default argument values to create a function. If we call the function hello() without arguments, it takes the default value as an argument.
Example:
Output: Hello my name is: Smith.
In the above example, the parameter $hello has a default value of “smith”. If we do not pass any value for this parameter when the function is called then this default value smith will be considered.
PHP Function With Return Value
In PHP, you can use a function with return values and the return statement. In PHP, the keyword return is used to return the value.
Example:
Output: The result is 160.
Reasons to use the PHP function
There are many reasons to use the PHP Function in our web Application: –
1. Reuse the Code:
The PHP functions are defined only once and can be called many times like the other programming languages. If we want to use a common code in different sections of the same program, it can be done by containing it inside a function and can be simply called whenever we required it. This will help us to reduce the time as well as effort of single code repetition. We can do it within a program as well as by importing a PHP file that contains the function and in some different programs.
2. Avoid repetition of codes:
PHP functions save a lot of code because we don’t need to write the logic many times. By the use of function, we can write the logic only once time and reuse it.
3. Readability:
In PHP, you can write individual functions and programming logic. So it is easy to understand and readable.
4. Comfortable maintenance:
If we are using functions in a program and need to change anything or any individual line of code, we can simply exchange it within the function. This particular change will be mirrored everywhere, wherever that particular function is used. That is why it is not difficult to maintain.
Conclusion
From the above information, it can be concluded that in PHP, the function is such a feature by which a user has the privilege to write their logical functions only once but can use them many times in their web development application. PHP also let the user use their predefined functions as well as customized user-defined functions. Users can create new user-defined functions and can use them for application purposes.
Learn other aspects of PHP through a proper and comprehensive PHP course and get yourself trained by the experts of the industry.