Home > Backend Development > PHP Tutorial > PHP Advanced Programming-Function-Zheng Aqi_PHP Tutorial

PHP Advanced Programming-Function-Zheng Aqi_PHP Tutorial

WBOY
Release: 2016-07-21 15:26:45
Original
1111 people have browsed it

1.php function
1. User-defined function

Copy code The code is as follows:

function function name ([ $parameter,[,…]])
{
//Function code
}

Note: The function name cannot have the same name as a system function or a function that has been defined by the user.
$parameter is a function parameter. A function generally can have 0 or more parameters.
2. Passing of parameters
Parameters are passed by value. For example, the func() function defined earlier is passed by the variable $ The values ​​of a and $b are passed. Passing parameters by value does not change the value outside the function because the parameter value inside the function changes.
Copy code The code is as follows:

function color(&$col) //Define function color()
{
$col="yellow";
}
$blue="blue";
color($blue); //Call function color(), parameters use Variable $blue
echo $blue; //Output "yellow"
?>

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

function sum()
{
$count=2;
}
sum();
echo $count;
?>

Since the variables in the function cannot act outside the function, so An error occurred when running the above, prompting that the $count variable is undefined.
4. Return value of function
When a function is declared, using the return statement in the function code can immediately end the running of the function. When the program returns, the next statement of the function is called.
Copy code The code is as follows:

function my_function($a=1)
{
echo $a;
return; //End the running of the function, the following statements will not be executed
$a++;
echo $a;
}
my_function() ; //Output 1
?>

Interrupting functions is not a common function of the return statement. Many functions use the return statement 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
can be called after the function is declared. In addition, if the function does not return value, just use the function name when calling. If a function has a return value, you can assign the function's return value to a variable.
Copy code The code is as follows:

//The function my_sort() that sorts an array in ascending order
function my_sort ($array)
{
for($i=0;$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); //Unsorted array
$sort_arr=my_sort($arr); //Assign the sorted array Give $sort_arr
foreach($sort_arr as $num)
echo $num; //Output 245679
?>

6. Recursive function
php supports recursion Functions, recursive functions call themselves, which can achieve the effect of looping.
Ask for 10!
For example:
Copy code The code is as follows:

function factorial($n)
{
if($n==0)
return 1; //If $n is 0, return 1
else
return $n*factorial($n1); //Recursive call until $n equals 0}
echo factorial(10); //Output 3628800
?>

Use recursion - in fact, recursion termination is given condition, otherwise the function will continue to execute until the memory is exhausted or the maximum number of calls is reached.
When using recursion, you must actually provide a recursion termination condition, otherwise the function will continue to execute until the memory is exhausted, or the maximum number of calls is reached.
7. Variable function
PHP has the concept of function variable. Adding a pair of parentheses after the variable forms a variable function.
$count();
8. System function
9. Example - Design a calculator program
Copy code The code is as follows:



Calculator Program







< select name="caculate">




function cac($a, $b, $caculate) //Define the cac function to calculate two numbers The result
{
if($caculate=="+") //If it is addition, the addition
return $a+$b;
if($caculate=="-") / /If it is subtraction
return $a-$b;
if($caculate=="*") //If it is multiplication, return product
return $a*$b;
if($caculate=="/")
{
if($b=="0") //Determine whether the divisor is 0
echo "The divisor cannot be equal to 0";
else
return $a/$b; //Divide if the divisor is not 0
}
}
if(isset($_POST['ok']))
{
$number1=$_POST['number1']; //Get number 1
$number2=$_POST['number2']; //Get number 2
$caculate=$_POST['caculate']; / /Get the operation action
//Call the is_numeric() function to determine whether the received string is a number
if(is_numeric($number1)&&is_numeric($number2))
{
//Call cac function calculation result
$answer=cac($number1,$number2,$caculate);
echo "<script>alert('".$number1.$caculate.$number2."=".$ answer."')</script>";
}
else
echo "<script>alert('The input is not a number! ')</script>";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323878.htmlTechArticle1.php function 1. User-defined function copy code The code is as follows: function function name ([$parameter,[ ,…]]) { //Function code} Note: The function name cannot be the same as the system function or user-defined...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template