PHP functions are just like similar to other programming languages. The function is a block of code that takes input in the form of a parameter and returns a value. A function is a recyclable piece or block of code that performs a specific action.
Read Also: PHP Loops
PHP Functions
In this article, we show different PHP functions. PHP functions are like other programming languages. A function is useful to perform a specific task in a program. The function can take input as an argument list and return value. There are various built-in functions available in PHP. Functions return values when called or can perform an operation without returning any value.
Advantages of PHP Functions
Reusability: If we have a common block of code that we would like to use many times in the program, we can contain it within the function and call it whenever required. It reduces the time of repetition of the code.
Easier Error Detection: Our Code divides into functions we can easily find in which function the error occurs and fix them fast and easily.
Easy Maintenance: As we use functions in our program, so if anything changes in the block of code, we can easily change it inside the function, and the change will reflect everywhere. Hence, easy to maintain.
PHP has two major types of functions
Built-in Function
PHP Provides us with a huge collection of built-in PHP libraries. Built-in functions already exist in PHP, which makes PHP a very efficient and productive scripting language.To use these functions we just need to call them as per our requirement like var_dump() ,fopen(),print_r(),gettype() etc.
User-Defined Functions
In addition to built-in functions, the PHP also allows us to define your functions called the user-defined functions. Using this, we can create our code package and use it wherever necessary simply by calling it.
Here are some advantage of using functions:
- Function reduces the repetition of code within a program.
- Function makes the code much easier to maintain.
- Functions make it easier to eliminate the errors.
- We can reuse functions in other applications.
Creating a Function
When we create a user-defined function, we need to keep some points in mind:
- Any name ending with an open and close parenthesis is function.
- The function keyword requires to write a function.
- The function name cannot begin with a number. It can start with an underscore or alphabet.
- A function name is not case sensitive.
Syntax
function function_name(){ //code to be executed }
Example
<?php function funchello(){ echo "Hello Good Morning"; } funchello(); ?>
Output
Hello Good Morning
Function Parameters or Arguments
We can pass the information in function through arguments which are separated by the comma operator.
Syntax
function function_name($first_parameter,$second_parameter){ // code to be executed }
Example
<?php function demo($num1, $num2, $num3){ $product = $num1 * $num2 * $num3; echo "The product is $product"; } demo(2, 3, 5); ?>
Output
The Product is 30
Function with Default values for function parameter
PHP allows us to set the default argument values for function parameters. If we do not pass any argument, then PHP will use the default set value for this parameter in the function call.
Example
<?php function demo($str, $num=12){ echo "$str is $num years old \n"; } demo("Rita", 15); demo("Neha"); ?>
Output
Rita is 15 years old Neha is 12 years old
In the above example, the $num has a default value 12; if we do not pass any value for this parameter, this default value 12 will be taken. Also, the $str has no default value, so it is compulsory.
Returning Values from Functions
Functions can also return values. The return keyword is useful to return value to the program. The returning value will be of any type, including objects, etc. The return value marks the end of the function, stops the execution after that, and returns it.
Example
<?php function demo($num1, $num2, $num3){ $product = $num1 * $num2 * $num3; return $product; } $retValue = demo(2, 3, 5); echo "The product is $retValue"; ?>
Output
The product is 30
Parameter Passing to Function
In PHP, two ways are using which we can pass the values into the function:
Pass by Value
Bypassing arguments using pass by value, the value of the argument gets changed, but the original value outside the function cannot be changed. It means a duplicate of the original value passes as an argument.
Pass by Reference
Bypassing arguments using pass by reference, the original value is passed. Therefore, the original value gets changed. In pass by reference, we pass the address of the value, where it is stored using ampersand sign(&).
Example
<?php // pass by value function demo1($num){ $num += 2; return $num; } // pass by reference function demo2(&$num){ $num += 10; return $num; } $n = 10; demo1($n); echo "The original value is $n \n"; demo2($n); echo "The original value changes to $n"; ?>
Output
The original value is 10 The original value changes to 20
Read More: PHP Arrays
Conclusion
I hope you have a great understanding of PHP functions.
Enjoy Learning PHP 🙂