![]() |
PHP function used to get values submitted through a form without using any database |
What is a PHP Function?
PHP Function: It is a collection of statements that performs a specific task. It executes when it is called by its name.
A large program is divided into several small building blocks for simplicity and this building block is called function.
The most important feature of function is code reusability. The PHP library provides many pre-defined functions.
The Advantage of Using PHP Function?
PHP functions give programmers a way to reuse their code more efficiently and productively.
The advantage of using PHP functions is that they allow developers to reuse code more efficiently and productively.
They use a
function library, which can be accessed by any developer who knows how to use
the library.
How To Create and Use PHP Function?
PHP functions give programmers a way to reuse their code more efficiently and productively.
The advantage of using PHP functions is that they allow developers to reuse code more efficiently and productively.
They use a
function library, which can be accessed by any developer who knows how to use
the library.
PHP is a general-purpose scripting language that is mainly
used for web development. It is the backbone of most new websites and
applications.
The goal of this tutorial is to show you how to create and
use PHP functions, which are reusable blocks of code that allow you to do more
with less code.
Syntax of PHP function?
function: It is a keyword that is used to declare the function in PHP.Function Name: Function name is the actual name of the function. we can use calling the function.
Parameter List
1. It is the place where we can pass some
parameters/variables.
2. These variables may be used in the program.
3. The value of the parameter is passed from the calling of function.
4. It is an optional part.
Body: It is the place where
the actual code is written to perform the specific task.
How To Create Function in Php?
<html>
<head>
<title>Writing PHP Function</title>
</head>
<body>
<?php
/* Create Function */
function writeMessage()
{
echo "Welcome To PHP Function You are really a good person, Have a good day!";
}
/* Calling Function */
writeMessage();
?>
</body>
</html>
*****OUTPUT*****
Welcome To PHP Function You are really a good person, Have a good day!
What are the Different Types of PHP Functions?
There are two types of functions in PHP.
1.Predefined Function
Predefined Function: The function which is predefined in the library is called predefined function. To the predefined function are sin(),cos(),tan() etc.
User-defined Function: The function which is made by the user is calling a user-defined function like add, sub, multiply, div, etc user-defined name.
Function with Parameter: Function parameter PHP gives the option to pass your parameters inside a function. You can pass as many parameters in the PHP function. These parameters work like variables inside the function.
How To Use Parameter Function in Php?
<html> <head> <title>PHP Function with Parameters</title> </head> <body> <?php function sumFunction($n1, $n2) { $sum = $n1 + $n2; echo "Sum of the two numbers is : $sum"; } sumFunction(50, 50); ?> </body> </html> *****OUTPUT***** Sum of the two numbers is :100 |
Function return type: The function can return the value using the return statement in conjunction with a value or object. it can be used to send the value function call.
How To Use return type function in PHP?
<html> <head> <title>PHP Function with Parameters</title> </head> <body> <?php function sumFunction($n1, $n2) { $sum = $n1 + $n2; return $sum } $return=sumFunction(50, 50); echo "Return Value From The Function : $return"; ?> </body> </html> *****OUTPUT***** Return Value From The Function :100 |
function with the default value : A function with the default value we can set a parameter to have a default value if the function's caller does not pass it.
How To Create defalut parameter in php?
<html> <head> <title>PHP Function Defalut Value</title> </head> <body> <?php function printdata($para = NULL) { print $para; } printdata("I Am Default Paramenter"); printMe(); ?> </body> </html> |
Arguments Passing by Reference: Arguments passing is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's values.
We can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition.
How To Create Arguments Passing by Reference in PHP?
<html>
<head><title>Passing Argument Reference</title>
</head><body><?phpfunction addFive($num) {
$num += 5;
}
function addSix(&$num) {$num += 6;
}
$orignum = 10;addFive( $orignum );echo "Original Value is $orignum<br />";addSix( $orignum );echo "Original Value is $orignum<br />";?></body></html>
*****OUTPUT*****Original Value is 10
Original Value is
16
|
Dynamic Function Calls: Dynamic Function is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.
How To Create Dynamic Function Call in Php?
<html><head>
<title>Dynamic Function Calls</title>
</head><body><?phpfunction say() {
echo "Hello Dynamic Functon call<br />";}$function_holder = "say";$function_holder();?></body></html>*****OUTPUT*****Hello Dynamic Function call |