php advanced programming functions aki cheng

  • 2020-05-09 18:18:12
  • OfStack

1. php function
1. User-defined functions
 
function  The function name ([$parameter,[, ... ]]) 
{ 
// Function code  
} 

Note: the function name cannot be the same as a system function or a user-defined function.
$parameter is the function parameter. Function 1 can have zero or more parameters.
2. Parameter transfer
Parameters are passed by value, for example, the func() function defined earlier is passed by the values of the variables $a and $b. Passing parameters by value does not change the value outside the function because the value inside the function changes.
 
<?php 
function color(&$col) // Define a function color() 
{ 
$col="yellow"; 
} 
$blue="blue"; 
color($blue); // Call a function color() , parameters use variables $blue 
echo $blue; // The 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 functions can only be used within functions. Variables defined in the main program
Can only be used in the main program, not in functions.
 
<?php 
function sum() 
{ 
$count=2; 
} 
sum(); 
echo $count; 
?> 

Since variables in the function cannot act outside the function, an error occurred at the top runtime, indicating that the $count variable is not defined.
4. The return value of the function
When a function is declared, return is used in the function code to immediately end the function, and the program returns the next statement that calls the function.
 
<?php 
function my_function($a=1) 
{ 
echo $a; 
return; // To close the function, the following statement will not be run  
$a++; 
echo $a; 
} 
my_function(); // The output 1 
?> 

Interrupt functions are not commonly used in return statements; many functions use the return statement to return a value to interact with the code that called them. < FONT color=#c0504d > The return value of a function can be of any type, including a list object < /FONT >
5. Function call
After the function declaration can be called, in addition, if the function does not return a value, call into the function name. If the function has a return value, you can assign the return value of the function to a variable.
 
// right 1 A function that sorts 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); // An unsorted array  
$sort_arr=my_sort($arr); // Assign the sorted array to $sort_arr 
foreach($sort_arr as $num) 
echo $num; // The output 245679 
?> 

6. Recursive functions
php supports recursive functions, which call themselves and can be looping.
Beg 10!
Such as:
 
<?php 
function factorial($n) 
{ 
if($n==0) 
return 1; // if $n for 0 It returns 1 
else 
return $n*factorial($n1); // Call recursively until $n Is equal to the 0 So far,  } 
echo factorial(10); // The output 3628800 
?> 

Using recursion 1 actually gives you a recursive termination condition, otherwise the function will execute 1 until memory runs out or the maximum number of calls is reached.
Using recursion 1 actually gives you a recursive termination condition, otherwise the function will execute 1 until memory runs out or the maximum number of calls is reached.
7. Function of variables
php has the concept of a function variable, which is a function of a variable by adding 1 pair of parentheses after the variable.
$count();
8. System functions
9. Example - design a calculator program
 
<html> 
<head> 
<title> Calculator program </title> 
</head> 
<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=" To calculate "> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
<?php 
function cac($a, $b, $caculate) // define cac Function to calculate the result of two Numbers  
{ 
if($caculate=="+") // If you add, you add  
return $a+$b; 
if($caculate=="-") // If you subtract, you subtract  
return $a-$b; 
if($caculate=="*") // If you return the product for the multiplication rule  
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; // Divisor is not 0 The division  
} 
} 
if(isset($_POST['ok'])) 
{ 
$number1=$_POST['number1']; // Get the number 1 
$number2=$_POST['number2']; // Get the number 2 
$caculate=$_POST['caculate']; // Get the action of the operation  
// call is_numeric() The function determines 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(' It's not a number! ')</script>"; 
} 
?> 

Related articles: