PHP functions are similar to functions in 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
Reusable Code: 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 wherever required. It reduces the time and length of repeating the code.
Easier Error Detection: When a code is divided into different functions, it is much easier to spot and detect errors and fix them.
Easy Maintenance: Codes arranged in blocks of functions are easy to maintain. A change made to a particular function is implemented throughout the code.
PHP has two major types of functions
Built-in Function
PHP has more than 1000 built-in functions. 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() [this functions dumps all information about one or more variables] ,fopen() [this function opens a file or a URL], print_r() [this function prints the information about a variable in a more human-readable way], gettype() [this function returns the type of variable] etc.
User-Defined Functions
In addition to built-in functions, PHP also allows us to define our functions. These functions are called user-defined functions. Using this, we can create our code package and use it wherever necessary simply by calling it.
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 a function.
- The function keyword is required while defining 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 that 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 the 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 of 12; if we do not pass any value for this parameter, the default value 12 will be taken. Also, $str has no default value, so it is compulsory.
Returning Values from Functions
Functions can also return values. The return keyword is used to return the 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, 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
Read Also: What Is Isset() Function in PHP and How to Use It
Parameter Passing to Function
In PHP, two ways are using which we can pass values into a function:
Pass by Value
By passing 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 gets passed as an argument.
Pass by Reference
By passing 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 the 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 <br>";
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
We hope you have been able to gain a complete understanding of PHP functions. Functions are an important tool for programming. If you are still unclear about some of the other basic concepts of PHP programming, you can read through our previous blogs. Happy Learning!