Home  >  Article  >  Backend Development  >  PHP User Defined Functions

PHP User Defined Functions

王林
王林Original
2024-08-29 12:47:59277browse

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

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.

Working of the PHP User Defined Function

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.

Examples for PHP User Defined Function

Example for PHP user-defined function without any parameter:

Example #1

Code:

<?php
// user-defined function definition
function printMessage(){
echo "Hello, How are you?";
}
//user-defined function call
printMessage();
?>

Output:

PHP User Defined Functions

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 #2

Example for PHP user-defined function to with required and default parameter –

Code:

<?php
// user-defined function definition
function sum($n1, $n2 = 0){
echo "The sum of the two numbers is: ";
echo $n1 + $n2 . "\n";
}
//user-defined function call
sum(20, 30);
sum(20);
?>

Output:

PHP User Defined Functions

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 #3

Example for PHP user-defined function to return a value from the function –

Code:

<?php
// user-defined function definition
function sum($n1, $n2 = 0){
return $n1 + $n2;
}
//user-defined function call
$result = sum(100, 50);
echo "The sum of the two numbers is: ";
echo $result . "\n";
$result = sum(200);
echo "The sum of the two numbers is: ";
echo $result . "\n";
?>

Output:

PHP User Defined Functions

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 #4

Example for PHP user-defined function to pass n number of parameters –

Code:

<?php
// user-defined function definition
function sum( ...$n ){
$sum = 0;
foreach ($n as $no){
$sum = $sum + $no;
}
return $sum;
}
//user-defined function call
$result = sum(200);
echo "The sum of the numbers is: ";
echo $result . "\n";
$result = sum(100, 50);
echo "The sum of the numbers is: ";
echo $result . "\n";
$result = sum(200, 50, 50);
echo "The sum of the numbers is: ";
echo $result . "\n";
?>

Output:

PHP User Defined Functions

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 #5

Example for PHP user-defined function to show call by reference without parameter –

Code:

<?php
// user-defined function definition
function Call_By_Reference( &$num ){
$nun = 0;
}
//user-defined function call
$n = 10;
echo "The value of n before calling the function is: ";
echo $n . "\n";
$result = Call_By_Reference($n);
echo "The value of n after calling the function is: ";
echo $n . "\n";
?>

Output:

PHP User Defined Functions

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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP Call by ReferenceNext article:PHP Call by Reference