This tutorial will introduce the syntax of function calling and function definition, and then talk about variables in functions and methods of passing numerical values to functions.
This tutorial will introduce the syntax of function calling and function definition, and then talk about variables in functions and methods of passing numerical values to functions.
1. Basics of functions
php tutorial provides a large number of functions and allows users to customize functions,
php function definition example
function myCount($inValue1,$inValue2)
{
$AddValue = $inValue1+$inValue2;
Return $AddValue; //Return the calculation result
}
$Count = myCount(59,100);
echo $Count; //Output 159
?>
Once a function is defined, it can be used anywhere.
2. Function parameters
php function The parameters are declared and defined when the function is defined. The function can have any number of parameters. The most commonly used transfer method is to pass by value. Or applied relatively rarely via references and default parameter values.
Example
function myColor ($inColor = "Blue")
{
Return "The color I like: $inColor.n";
}
echo myColor();
echo myColor("pink");
?>
Generally the value passed will not change due to internal changes in the function. Unless it is a global variable or reference, let’s look at php function reference instance
function str_unite (&$string)
{
$string .= 'I also like blue.';
}
$str = 'Like red,';
str_unite ($str);
echo $str; // Output result: 'I like red and I also like blue.'
?>
Global variables
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>