this article illustrates how PHP uses a function static variable to implement the specified number of iterations. I want to share with you the following details:

in PHP, in addition to the static member attribute of class, in function, static can also be used to define static variables. Thus it is convenient to complete the function iteration.

1:

 < PHP function Test (?) {$a = 0; echo $a; $a++ > 

;}? In this case, each call to Test function, a was re assigned to 0, because once the exit function variable a will be re assigned to 0, because once the exit the function of the variable a would not exist. If you want to complete the iteration, need to write a will not lose the count of the counting function, should be defined as static $a variables:

 < PHP function test (?) static {$a = 0; echo $a; $a++ > 

;}? This $a is assigned to the first call when, after each call 1, and will not be overridden.

so you can use this feature, for a specified number of iterative operation:

2: (an array access with pop-up 5 element of the results of

 $arr) = range (1,10,1); function test ($arr) {static $count=0; array_pop ($ARR); $count++ ($count; if < {test; 5) ($arr);}else{var_dump ($arr); exit test;}} ($arr); 

 array operation results: (5) int (1) {[0]=> [1]=> [2]=> int (2) int (3) [3]=> int (4) ([4]=> int 5)} 

more PHP on the relevant content of interested readers can see the special station: "php string (string) " PHP "data structure and algorithm tutorial " and "php program design "," php , "sorting algorithm summary" PHP commonly used traversal algorithm and skills summary "," PHP "math skills summary," PHP array (Array) operation skills Daquan "" php "common database operation skills summary

hope to help the PHP programming.

you might be interested in this article:


This concludes the body part

This paper fixed link:http://www.script-home.com/php-using-function-static-variables-to-implement-the-number-of-specified-iterations.html | Script Home | +Copy Link

Article reprint please specify:PHP using function static variables to implement the number of specified iterations | Script Home

You may also be interested in these articles!