PHP allows you to create user-defined functions, where you can create your functions. A function is a collection of statements that can be used over and over again in a program. A call to a function will cause it to be run. There are several built-in functions in PHP, such as mathematical, string, date, and array functions. A function can also be defined to meet a specific requirement. The term “user-defined function” refers to such a function. A function does not run when it is defined; instead, it runs when it is called.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax to create a PHP user defined function –
A PHP user-defined function declaration starts with the keyword function as shown below –
function funName($arg1, $arg2, ... $argn) { // code to be executed inside a function //return $val }
funName – This is the name of the function. The name of a function must begin with a letter or an underscore. There is no case sensitivity in function names. Optional but any number of arguments can be used to define a function. When calling, however, you must provide the same number of parameters.
The syntax to call a PHP user-defined function –
$ret=funName($arg1, $arg2, ... $argn);
After calling a function the return value of the function stores into the $ret variable.
The working of the PHP user-defined function:
A user-defined function’s declaration begins with the word function, then the name of the function you want to write, followed by parentheses (), and lastly your function’s body or code enclosed in curly brackets. The body of a function can contain any acceptable PHP code, such as conditionals, loops, and all. Suppose we have defined a function as “function printMessage(){ echo “Hello World!”;}”. Next, we will call the printMessage() function to display the message as “printMessage()”. The code will print the message on the screen. Then, regardless of whether the last line of the function block is a return, program control returns to the place from where it was invoked after the statements in the block have been executed.
Example for PHP user-defined function without any parameter:
Code:
Output:
As in the above program,the printMessage() function is created using the keyword function. The function prints the message “Hello, How are you?”. So, farther in the program when it is calling the function as “printMessage();”. It prints a message, as we can see in the above output.
Example for PHP user-defined function to with required and default parameter –
Code:
Output:
As in the above program,the sum() function is created. The function accepts two parameters, where one parameter is must require and the other parameter is optional when we do not pass the value for it, then it takes the default value. Farther in the program when it is calling the function as “sum(20, 30);”, it prints the sum 50. When it is calling “sum(20);”, it prints the sum 20, as we can see in the above output.
Example for PHP user-defined function to return a value from the function –
Code:
Output:
As in the above program,the sum() function is created. The function accepts two parameters, where one parameter is must require and the other parameter is optional when we do not pass the value for it, then it takes the default value. Next, after performing the summation of numbers the function returns it. Farther in the program when it is calling the function as “sum(100, 50);”, it returns the sum 150. When it is calling “sum(200);”, it returns the sum 200, as we can see in the above output.
Example for PHP user-defined function to pass n number of parameters –
Code:
Output:
As in the above program,the sum() function is created. The function can accept as many parameters as you pass. Next, after performing the summation for passed numbers the function returns it, as we can see in the above output.
Example for PHP user-defined function to show call by reference without parameter –
Code:
Output:
As in the above program,the sum() function is created. The function accepts one parameter which stores the address of the passed variable or reference to that variable. Inside the function, the value of the passed variable is changed to 0 as the variable num stores the address of the passed variable n.Farther in the program the variable n value is printing before and after calling the function and the value of variable n is changed, as we can see in the above output.
There are several built-in functions in PHP, but still, the user can define a function to meet a specific requirement is called a user-defined function. A function is a collection of statements that can perform a specific task and can be used over and over again in a program.
The above is the detailed content of PHP User Defined Functions. For more information, please follow other related articles on the PHP Chinese website!