This article mainly introduces the php function and the method for passing parameters, and analyzes the specific usage of function passing by value in the form of examples, which has good reference value, for more information about function calling and function definition syntax, see the examples in this article. share it with you for your reference. The details are as follows:
I. Basics of functions
Php provides a large number of functions and allows you to customize them. The php function definition instance code is as follows:
The code is as follows:
<? Php
Function myCount ($ inValue1, $ inValue2)
{
$ AddValue = $ inValue1 + $ inValue2;
Return $ AddValue; // return the calculation result
}
$ Count = myCount (59,100 );
Echo $ Count; // output 159
?>
Functions can be used anywhere once defined.
II. function parameters
Php function parameters are declarative during function definition. a function can have any number of parameters. The most common transfer method is passed by value, or the default parameter values are applied less by reference. the instance code is as follows:
The code is as follows:
<? Php
Function myColor ($ inColor = "blue ")
{
Return "my favorite color: $ inColor .";
}
Echo myColor ();
Echo myColor ("pink ");
?>
Generally, the passed value will not change because of internal changes of the function. unless it is a global variable or reference, let's look at the php function reference instance. the code is as follows:
The code is as follows:
<? Php
Function str_unite (& $ string)
{
$ String. = 'also like blue .';
}
$ Str = 'like red ,';
Str_unite ($ str );
Echo $ str; // output result: 'Like red or blue .'
?>
The code for global variables is as follows:
The code is as follows:
<? Php
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>
I hope this article will help you with PHP programming.