Php advanced programming-function-Zheng Achi

Source: Internet
Author: User
Php advanced programming-function-Zheng Achi. For more information about php learning, see. 1. php functions
1. user-defined functions
The code is as follows:
Function name ([$ parameter, [,…])
{
// Function Code
}

Note: the function name cannot be the same as that of a system function or a defined function.
$ Parameter is a function parameter. generally, a function can have 0 or more parameters,
2. passing parameters
Parameters are passed through values. for example, the previously defined func () function is passed through the values of $ a and $ B. Passing a parameter through a value does not change the value outside the function due to changes in the internal value of the function.
The code is as follows:
Function color (& $ col) // defines the function color ()
{
$ Col = "yellow ";
}
$ Blue = "blue ";
Color ($ blue); // call the function color (). The parameter uses the variable $ blue.
Echo $ blue; // output "yellow"
?>

3. function variable scope
The variables defined in the main program and those defined in the function are both local variables. The variables defined in the function can only be used inside the function. Variables defined in the main program
It can only be used in the main program, but not in the function.
The code is as follows:
Function sum ()
{
$ Count = 2;
}
Sum ();
Echo $ count;
?>

Because the variables in the function cannot act on the outside of the function, an error occurs during the above running, prompting that the $ count variable is undefined.
4. function return value
When a function is declared, you can use the return statement in the function code to immediately end the function operation. when the program returns, the next statement that calls the function.
The code is as follows:
Function my_function ($ a = 1)
{
Echo $;
Return; // end the function. the following statement will not be run.
$ A ++;
Echo $;
}
My_function (); // output 1
?>

Interrupt functions are not common functions of return statements. many functions use return statements to return a value to interact with the code that calls them. The return value of a function can be of any type, including list objects.
5. function call
The function can be called after the function declaration. if the function does not return a value, call the function name. If a function has a return value, you can assign the return value of the function to a variable.
The code is as follows:
// The function my_sort () for sorting an array in ascending order ()
Function my_sort ($ array)
{
For ($ I = 0; $ I {
For ($ j = $ I + 1; $ j {
If ($ array [$ I]> $ array [$ j])
{
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ I];
$ Array [$ I] = $ tmp;
}
}
}
Return $ array;
}
$ Arr = array (6, 4, 7, 5, 9, 2); // unordered array
$ Sort_arr = my_sort ($ arr); // assign the sorted array to $ sort_arr
Foreach ($ sort_arr as $ num)
Echo $ num; // output 245679
?>

6. recursive functions
Php supports recursive functions. recursive functions are called by themselves to implement loops.
10!
For example:
The code is as follows:
Function factorial ($ n)
{
If ($ n = 0)
Return 1; // if $ n is 0, 1 is returned.
Else
Return $ n * factorial ($ n limit 1); // recursive call until $ n equals 0}
Echo factorial (10); // output 3628800
?>

In fact, recursive termination conditions must be given when you use the normalization function. Otherwise, the function continues until the memory is exhausted or the maximum number of calls is reached.
In fact, recursive termination conditions must be given when you use the normalization function. Otherwise, the function continues until the memory is exhausted or the maximum number of calls is reached.
7. variable functions
Php has the function variable concept. adding a pair of parentheses after the variable constitutes a variable function.
$ Count ();
8. system functions
9. instance-design a calculator program
The code is as follows:


Calculator program





Function cac ($ a, $ B, $ caculate) // defines the cac function, which is used to calculate the result of two numbers.
{
If ($ caculate = "+") // if it is the addition of the add rule
Return $ a + $ B;
If ($ caculate = "-") // if it is a subtraction rule, subtract
Return $ a-$ B;
If ($ caculate = "*") // returns the product if it is multiplication.
Return $ a * $ B;
If ($ caculate = "/")
{
If ($ B = "0") // determines whether the divisor is 0.
Echo "the divisor cannot be equal to 0 ";
Else
Return $ a/$ B; // Division if the divisor is not 0
}
}
If (isset ($ _ POST ['OK'])
{
$ Number1 = $ _ POST ['number1']; // get the number 1
$ Number2 = $ _ POST ['number2']; // get the number of 2
$ Caculate = $ _ POST ['caculate']; // Obtain the operation.
// Call the is_numeric () function to determine whether the received string is a number.
If (is_numeric ($ number1) & is_numeric ($ number2 ))
{
// Call the calculation result of the cac function
$ Answer = cac ($ number1, $ number2, $ caculate );
Echo "script" alert ('". $ number1. $ caculate. $ number2." = ". $ answer."') script ";
}
Else
Echo "script" alert ('The input is not a number! ') Script ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.