Home  >  Article  >  Backend Development  >  Example analysis of php functions and passed parameters, analysis of php function examples_PHP tutorial

Example analysis of php functions and passed parameters, analysis of php function examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:58808browse

Example analysis of php functions and parameters passed, analysis of php function examples

The example in this article describes the function calling and function definition syntax, and explains the variables in the function and the method of passing numerical values ​​to the function. Share it with everyone for your reference. The details are as follows:

1. Basics of functions

php provides a large number of functions and allows users to customize functions. The example code of php function definition is as follows:

Copy code The code is as follows:
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 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 by reference and default parameter values ​​are relatively rarely used. Example code As follows:

Copy code The code is as follows:
function myColor ($inColor = "Blue")
{
Return "The color I like: $inColor. ";
}
echo myColor();
echo myColor("pink");
?>

Generally, the value passed will not be changed due to internal changes in the function, unless it is a global variable or reference. Let's look at the PHP function reference example. The code is as follows:
Copy code The code is as follows:
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, the code is as follows:
Copy code The code is as follows:
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912283.htmlTechArticleAnalysis of examples of php functions and passed parameters, analysis of php functions examples. The examples in this article describe the function calling and function definition syntax, And explained about variables in functions and passing values ​​​​to functions...
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