PHP function is a piece of code that can be reused several times It can take input as the logic list and return value. PHP has thousands of built-in functions
In PHP, we can also define functional function, functions within function and recursive function.
Syntax -
function functionname(){
//code to be executed
}
Example-
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
PHP Function Arguments
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list
Example-
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
Call By Reference -
The value value near the function does not modify the actual value by default (value by call). But we can do this by giving recognition as a reference.
By default, the value given to the function is called value. To pass the reference as the value, you must use the ampersand (&) symbol before the logic name.
Example-
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
0 comments:
Post a Comment