Php function -: Like any other language function are same to other function.The real power comes its from php function. Php has more then 1000 function. A function is a small code which takes one more input in the form of parameter and does some processing and return a value.
In other words we can say that A function is a block of statements that can be used repeatedly in a program.A function will not execute immediately when a page loads.A function will be executed by a call to the function.
There are two main part of function.
- Creating a php function
- Call a function in php
How to create a function in php -: php provide the facility for user define function.We can create user define function in php. A user define function is start with the word “function”.
For example of user define function in php-
<html> <head> <title>PHP Function</title> </head> <body> <?php function netnic() { echo "welcome in this website"; } netnic(); ?> </body> </html>
The result of this program
welcome in this website
Php function with parameter -: php provide this facility to pass your parameter inside a function.This parameter works as like a variable in the program. for example
<html> <head> <title>Writing PHP Function which returns value</title> </head> <body> <?php function netnic($num1, $num2, $num3) { $total = $num1 + $num2 + $num3; echo "Sum of the three numbers is : $total"; } netnic(20, 40, 70); ?> </body> </html>
Output of this program
Sum of the three numbers is : 130
Passing arguments in php -: In the php a information can be passed by function arguments.A reference to the variable is manipulated by the function rather than a copy of the variable’s value. You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition. For example
<html> <head> <title>passing arguments with return value</title> </head> <body> <?php function addFive($num) { $num += 5; } function addSix(&$num) { $num += 6; } $netnic = 10; addFive( $netnic ); echo "Original Value is $netnic<br />"; addSix( $netnic ); echo "Original Value is $netnic<br />"; ?> </body> </html>
Output of this program
Original Value is 10 Original Value is 16
Returning function value in php-: A function has a return value with return statement .You can return value one or more then using the array in php. For example
<?php function netnic($num1, $num2, $num3) { $total = $num1 + $num2 + $num3; return $total; } $return_value = netnic(10, 20, 30); echo "Returned value from the function : $return_value"; ?>
The output of this program
Returned value from the function : 60
Function parameter set default value -: You can set a parameter to have a default value if the function that the value of function could not pass. For example
<?php function netnic($param = NULL) { print $param; } netnic("This is testing "); netnic(); ?>
Output of this program
This is testing
How to call dynamic function in php-Some time that possible to assign function names as strings to variables and then behavior of these variables exactly as you would the function name itself.For like this example
<?php
function netnic() {
echo "Hello mr vikram singh <br />";
}
$function_holder = "netnic";
$function_holder();
?>
The output of this program
Hello mr vikram singh