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
Index
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.
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 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.
When we create a user-defined function, we need to keep some points in mind:
Syntax
function function_name(){ //code to be executed }
Example
<?php function funchello(){ echo "Hello Good Morning"; } funchello(); ?>
Output
Hello Good Morning
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
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.
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
In PHP, two ways are using which we can pass values into a function:
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.
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
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!
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…