Analysis of php functions and passing parameter instances, and analysis of php function instances. Php function and transfer parameter instance analysis, php function instance analysis this article describes the function call and function definition syntax, this article also explains how to analyze the variables in the function, transfer numeric values to the function, and transfer parameter instances.
This article describes the function call and function definition syntax, explains the variables in the function, and the numeric method for passing values to the function. 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.
Examples in this article describes the function call and function definition syntax, and explains the variables in the function and the transfer of values to the function...