Home  >  Article  >  Daily Programming  >  How to implement recursive sorting in PHP

How to implement recursive sorting in PHP

藏色散人
藏色散人Original
2018-09-21 16:33:366265browse


This article mainly introduces to you the three implementation methods of PHP recursive sorting and PHP recursive algorithm.

How to implement recursive sorting in PHP

Recursive algorithms should be familiar to any programmer. Because the concept of recursion, whether in PHP language or other programming languages ​​such as Java, is the soul of most algorithms.

For PHP novices, the implementation principle of recursive algorithms may not be easy to understand. But as long as you understand and master the principle of this algorithm, you can flexibly use recursive algorithms to implement various functions in programming, such as infinite classification. Recursion is also a basic algorithm skill that beginners need to master most.

So what exactly is recursion?

Recursion is actually a programming method in which the function itself directly or indirectly calls itself. It can also be understood as a method with repeated execution process. This is very similar to a loop, but the recursive call There must be a termination condition in the function, that is, there must be a condition to break out of the repeated execution process, otherwise it will become an infinite loop.

Below we will introduce to you the PHP recursive algorithm and the three implementation methods of PHP recursive sorting through specific code examples.

Method 1: Static variables

<?php
function call(){
    static $i=1;
    echo  $i.&#39;<br>&#39;;
    $i++;
    if ($i<=10){
        call();
    }
}
call();

In this method, we mainly use static to define static variables to implement recursive sorting. As above, we have defined a call method and static variable $i. If we do not add a judgment to the $i variable, but run it directly, an infinite loop will obviously occur.

So here we add an if conditional judgment statement. Finally, it calls its own method in a loop, and the result is as shown in the figure below:

How to implement recursive sorting in PHP

As shown in the figure, the effect of using static variables to achieve recursive sorting is achieved.

Method 2: Global variables

$i=1;
function call(){
    global $i;
    echo $i;
    $i++;
    if($i<=10){
        call();
    }
}
call();

This method mainly uses global to define global variables to implement PHP recursive sorting. As above, we first define an $i variable, and then create a call method. In this method, $i is defined as a global variable, and then the final result of calling its own method in a loop is the same as the above result:

12345678910

Method 3: Parameter passing by reference

function call(&$i=1){
  echo $i.&#39;<br>&#39;;
  $i++;
  if($i<=10){
       call($i);
   }
}
call();

When you use this method, you can briefly understand the concept of passing by reference in PHP: you can pass a variable to a function by reference, so that the function The values ​​of its parameters can be modified. Using reference parameters to implement PHP recursive sorting is the most basic and simple algorithm.

Note: When calling your own method, you must pass the parameters in, otherwise an error will be reported.

The above are the three implementation methods of PHP's recursive algorithm, that is, recursive sorting. Hope it helps those in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!


The above is the detailed content of How to implement recursive sorting in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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