PHP Advanced Programming-function-Zheng Achi _php Foundation

Source: Internet
Author: User
1.php function
1. User-defined Functions
Copy Code code as follows:

function name ([$parameter, [,...]])
{
function code
}

Note: The function name cannot be the same as a system function or a function that the user has already defined.
$parameter is a function argument, a function can typically have 0 or more arguments.
2. Transfer of parameters
Parameters are passed by value, such as the previously defined func () function is passed through the values of the variable $a and $b. Passing a parameter by value does not change the value outside the function because of the change in the value of the function's internal parameter.
Copy Code code as follows:

<?php
function color (& $col)//define functions color ()
{
$col = "Yellow";
}
$blue = "Blue";
Color ($blue); Call function color (), parameter uses variable $blue
Echo $blue; Output "Yellow"
?>

3. Scope of function variables
Variables defined in the main program and variables defined in the function are local variables. Variables defined in a function can only be used within a function. Variables defined in the main program
Can be used only in the main program, not in functions.
Copy Code code as follows:

<?php
function sum ()
{
$count = 2;
}
SUM ();
Echo $count;
?>

Because the variables in the function cannot be used outside of the function, there is an error running above, prompting the $count variable to be undefined.
4. return value of function
function declaration, use the return statement in the function code to immediately end the function's operation, and the program returns to the next statement of the function.
Copy Code code as follows:

<?php
function my_function ($a =1)
{
echo $a;
Return End Function running, the following statement will not be run
$a + +;
echo $a;
}
My_function (); Output 1
?>

An interrupt function is not a common function of a return statement, and many functions use the returns statement to back up a value to interact with the code that invokes them. The return value of the <font color= #c0504d > function can be any type, including list objects </FONT>
5. Call to function
Can be invoked after the function declaration, and if the function does not return a value, call into the USE function name. If the function has a return value, you can assign the return value of the function to a variable.
Copy Code code as follows:

function to sort an array in ascending order My_sort ()
function My_sort ($array)
{
for ($i =0; $i <count ($array); $i + +)
{
for ($j = $i +1; $j <count ($array); $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); Assigns the sorted array to the $sort_arr
foreach ($sort _arr as $num)
Echo $num; Output 245679
?>

6. Recursive functions
PHP supports recursive functions, recursive functions are called themselves, you can achieve the role of the loop.
Please 10!
For example:
Copy Code code as follows:

<?php
function factorial ($n)
{
if ($n ==0)
return 1; Return 1 if $n is 0
Else
return $n *factorial ($n  1); Recursive call until $n equals 0}
echo factorial (10); Output 3628800
?>

Use recursive one in fact, to give a recursive termination condition, the function will go on until the memory runs out or the maximum number of calls is reached.
Use recursive one in fact, to give a recursive termination condition, the function will go on until the memory runs out or the maximum number of calls is reached.
7. Variable function
PHP has the concept of function variables, a variable followed by a pair of parentheses to form a variable function.
$count ();
8. System function
9. Example-Design a calculator program
Copy Code code as follows:

<title> Calculator program </title>
<body>
<form method=post>
<table>
<tr><td><input type= "Text" size= "4" name= "Number1" >
<select name= "Caculate" >
<option value= "+" >+
<option value= "-" >-
<option value= "*" >*
<option value= "/" >/
</select>
<input type= "Text" size= "4" name= "Number2" >
<input type= "Submit" Name= "OK" value= "calculation" >
</td>
</tr>
</table>
</form>
</body>
<?php
function CAC ($a, $b, $caculate)//define a CAC function to compute the two-digit result
{
if ($caculate = = "+")///If addition is added
return $a + $b;
if ($caculate = "-")//Subtract if subtraction
return $a-$b;
if ($caculate = = "*")//If the product is returned for multiplication
return $a * $b;
if ($caculate = = "/")
{
if ($b = = "0")//judge whether the divisor is 0
echo "Divisor cannot equal 0";
Else
return $a/$b; Divide by not 0
}
}
if (Isset ($_post[' OK '))
{
$number 1=$_post[' Number1 ']; Get the number 1
$number 2=$_post[' number2 ']; Get the number 2
$caculate =$_post[' caculate ']; Get the action of the Operation
Call the Is_numeric () function to determine whether the received string is a number
if (Is_numeric ($number 1) &&is_numeric ($number 2))
{
Call the CAC function to compute the result
$answer =CAC ($number 1, $number 2, $caculate);
echo "<script>alert". $number 1. $caculate. $number 2. " = ". $answer." ') </script> ";
}
Else
echo "<script>alert (' 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.