Php functions and passing parameters this tutorial introduces the call and definition syntax of functions, as well as the variables in functions and the numeric method for passing functions.
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:
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:
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:
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:
$ A = 1;
$ B = 2;
Function Sum ()
{// Open source code phpfensi.com
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>