What is PHP Function and how to use with example?
PHP function work as other programming languages means that PHP Functions also similar to other programming Functions.
Functions we can define as its a piece of code which takes one more input in the form of the parameter and does some processing and finally it returns a value.
here, PHP gives you choose to create own functions as well.
In the programming, PHP Functions used as two parts in which should be clear to you like
Why use Functions?
Better code organization –
PHP functions allow us to perform a specific task together with group blocks of related code.
Reusability –
A function we can be called by a number of scripts in our PHP files program.
This saves us time of reinventing the wheel where we want to perform some routine tasks like as connecting to the database
Easy maintenance-
its updates to the system, when only need to be made in one place.
Built-in Functions
In PHP Package functions have existed installation package because Built-in functions are functions
PHP a very efficient and productive scripting language.
The built-in functions can be categorized into String Functions and Numeric Functions.
String Functions
These are those functions which manipulate string data, refer to the article on strings for implementation examples of string functions
Numeric Functions
Numeric functions are those function which returns numeric results.
Numeric PHP function can be used to return constants, format numbers, perform mathematical computations etc.
create your own functions
Calling a PHP Function
You know, IN PHP have more than 1000 Function which built-in library, functions created for the different area.
so, when you need just call them according to your requirement.
In most cases or area, you have no need to create own function
How to Create a PHP Function?
Its very simple to create own PHP function in programming
Suppose that you want to create an own PHP function which will write a message on your browser when you will call it.
Example:-
Let’s create a function called simpleMessage() after that calls it just creating it.
one thing is important here while creating a function its name should start with keyword function, and put all the PHP code inside { and } braces
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<html> <head> <title>Write own PHP Functions for programming</title> </head> <body> <?php /* Defining a PHP Function */ function simpleMessage() { echo "Tutorialscan is a good website for Learning!"; } /* Calling a PHP Function */ simpleMessage(); ?> </body> </html> |
Finally, display the following result −

Passing Arguments by Reference
In PHP It is possible to pass arguments to functions by reference.
Therefore, means that when a reference to the variable is manipulated by the function rather than a copy of the variable’s value.
In cases, Any changes made to an argument will change the value of the original variable.
SO, here in PHP, you can pass an argument by reference by adding an ampersand to the variable name in either the function call.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <head> <title>Passing Argument by Reference</title> </head> <body> <?php function addSeven($num) { $num += 7; } function addEight(&$num) { $num += 8; } $orignum = 13; addSeven( $orignum ); echo "Original Value is $orignum<br />"; addEight( $orignum ); echo "Original Value is $orignum<br />"; ?> </body> </html> |
PHP Functions with Parameters
you can pass your parameters inside a function because of PHP Give you the option.
You have the option to pass as many as parameters your choice.
these parameters work as like variables inside the function
Let’s see the example we take three integer parameters and add them together and then print
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html> <head> <title>Writing PHP Function with Parameters</title> </head> <body> <?php function addFunction($num1, $num2, $num3) { $sum = $num1 + $num2 + $num3; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20, 30); ?> </body> </html> |

PHP Functions returning value
In PHP When use return statement then a function return a value in conjunction with a value or object.
Here, in statement return stops the execution of the function and also sends the value back to the calling code.
using return array(1,2,3,4) then you can return more than one value from a function
see given below example here, takes three integer parameters and add them together and then returns their sum to the calling program.
one thing notice here, return keyword is used to return a value from a function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<html> <head> <title>Writing a PHP Function which returns value</title> </head> <body> <?php function addFunction($num1, $num2, $num3) { $sum = $num1 + $num2 $num3; return $sum; } $return_value = addFunction(10, 20, 30); echo "Returned value from the function : $return_value"; ?> </body> </html> |

Dynamic Function Calls
In PHP It’S possible to assign function names as strings to variables after that treat these variables exactly as you would the function name itself.
given below example shows this behavior.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html> <head> <title>Dynamic Function Calls</title> </head> <body> <?php function sayWelcome() { echo "Hello<br />"; } $function_holder = "sayWelcome"; $function_holder(); ?> </body> </html> |

Setting Default Values for Function Parameters
In PHP, we can set a parameter to have a default value when the function’s caller doesn’t pass it.
Here, function prints NULL in this case use does not pass any value to this function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html> <head> <title>Write PHP Function which returns value</title> </head> <body> <?php function printMe($param = NULL) { print $param; } printMe("This is dummy testing"); printMe(); ?> </body> </html> |
