Home Daily Programming PHP Knowledge How to implement recursive sorting in PHP

How to implement recursive sorting in PHP

Sep 21, 2018 pm 04:33 PM


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();
Copy after login

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();
Copy after login

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
Copy after login

Method 3: Parameter passing by reference

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)