Home Backend Development PHP Tutorial PHP recursion, static variables, anonymous function use

PHP recursion, static variables, anonymous function use

Dec 07, 2016 pm 04:01 PM

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Current To-Do List</title>
</head>
<body>
<?php
    function make_list($parent) {
        global $tasks;
        echo &#39;<ol>&#39;;
        foreach($parent as $task_id => $todo) {
            echo "<li>$todo";
            if (isset($tasks[$task_id])) { // 如果当前id有子任务则递归创建菜单
                make_list($tasks[$task_id]);
            }
            echo "</li>";
        }
        echo &#39;</ol>&#39;;
    }
    $db = mysqli_connect(&#39;192.168.31.172&#39; ,&#39;root&#39;, &#39;root&#39;, &#39;phpadvanced&#39;);
    mysqli_query($db, "set names utf8");
    $q = &#39;SELECT task_id, parent_id, task FROM tasks WHERE date_completed="0000-00-00 00:00:00" 
    ORDER BY parent_id, date_added ASC&#39;;
    $r = mysqli_query($db, $q);
    $tasks = array();
while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) {
    $tasks[$parent_id][$task_id] = $task;
}
//echo &#39;<pre class="brush:php;toolbar:false">&#39;.print_r($tasks,1).&#39;
'; make_list($tasks[0]); //把包含顶级任务的数组发送给它, 最顶级的parent_id是0 ?>
Copy after login

Static variable statistics code

<?php
// create the array.
// Array structs
// StudentId = > ["name" => "Name", "grade" => xx.x];
$students = [
    256 => [&#39;name&#39; => &#39;Jon&#39;, &#39;grade&#39; => &#39;98.5&#39;],
    2 => [&#39;name&#39; => &#39;Vance&#39;, &#39;grade&#39; => &#39;85.1&#39;],
    9 => [&#39;name&#39; => &#39;Stephen&#39;, &#39;grade&#39; => &#39;94.0&#39;],
    364 => [&#39;name&#39; => &#39;Steve&#39;, &#39;grade&#39; => &#39;85.1&#39;],
    68 => [&#39;name&#39; => &#39;Rob&#39;, &#39;grade&#39; => &#39;74.6&#39;],
];
function name_sort($x, $y) {
    static $count = 1;
    echo "<p>Iteration $count: {$x[&#39;name&#39;]} vs. {$y[&#39;name&#39;]} </p>\n";
    $count ++;
    return strcasecmp($x[&#39;name&#39;], $y[&#39;name&#39;]);
}
function grade_sort($x, $y) {
    static $count = 1;
    echo "<p>Iteration $count: {$x[&#39;grade&#39;]} vs. {$y[&#39;grade&#39;]}</p>\n";
    $count++;
    return $x[&#39;grade&#39;] < $y[&#39;grade&#39;];
}
echo print_r($students, 1);
uasort($students, &#39;name_sort&#39;); //保持键并使用自定义排序
echo print_r($students, 1);
uasort($students, &#39;grade_sort&#39;);
echo print_r($students, 1);
?>
Copy after login

Anonymous function - closure code

<?php
// create the array.
// Array structs
// StudentId = > ["name" => "Name", "grade" => xx.x];
$students = [
    256 => [&#39;name&#39; => &#39;Jon&#39;, &#39;grade&#39; => &#39;98.5&#39;],
    2 => [&#39;name&#39; => &#39;Vance&#39;, &#39;grade&#39; => &#39;85.1&#39;],
    9 => [&#39;name&#39; => &#39;Stephen&#39;, &#39;grade&#39; => &#39;94.0&#39;],
    364 => [&#39;name&#39; => &#39;Steve&#39;, &#39;grade&#39; => &#39;85.1&#39;],
    68 => [&#39;name&#39; => &#39;Rob&#39;, &#39;grade&#39; => &#39;74.6&#39;],
];
echo print_r($students, 1);
uasort($students, function($x, $y){
    return strcasecmp($x[&#39;name&#39;], $y[&#39;name&#39;]);
}); //保持键并使用自定义排序
echo print_r($students, 1);
uasort($students, function($x, $y){
    return $x[&#39;grade&#39;] < $y[&#39;grade&#39;];
});
echo print_r($students, 1);
?>
Copy after login

Passing parameters by reference, used when the data is a big data, there is no need to copy a copy of the data code

<?php
function increment(&$i) {
    $i++;
}
$num = 2;
increment($num);
echo $num;
?>
Copy after login

The above is PHP recursion , Static variables, content used by anonymous functions, for more related content, please pay attention to the PHP Chinese website (www.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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Usage and characteristics of C++ anonymous functions Usage and characteristics of C++ anonymous functions Apr 19, 2024 am 09:03 AM

An anonymous function, also known as a lambda expression, is a function that does not specify a name and is used for one-time use or to pass a function pointer. Features include: anonymity, one-time use, closures, return type inference. In practice, it is often used for sorting or other one-time function calls.

Analysis of anonymous function application scenarios in Golang functions Analysis of anonymous function application scenarios in Golang functions May 16, 2023 pm 10:51 PM

As a modern programming language, Golang (also known as Go language) has many powerful features. Among them, anonymous functions are a very important concept in Golang and are widely used in various scenarios. In this article, we will deeply analyze the application scenarios of anonymous functions in Golang functions. Event Handler In event handler, anonymous function is a very convenient and practical tool. Custom logic can be passed to the event handler through an anonymous function, such as: funcmain(){bt

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Oct 21, 2023 am 10:21 AM

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Before PHP7, we often used functions to encapsulate a specific piece of logic, and then called these functions in the code to implement specific functions. However, sometimes we may need to define some temporary logic blocks in the code. These logic blocks do not need to create an independent function, and at the same time, we do not want to introduce too many global variables into the code. PHP7 introduces anonymous functions and closures, which can solve this problem very well. An anonymous function is a function without a name

Can Golang anonymous functions return multiple values? Can Golang anonymous functions return multiple values? Apr 13, 2024 pm 04:09 PM

Yes, anonymous functions in Go language can return multiple values. Syntax: func(arg1,arg2,...,argN)(ret1,ret2,...,retM){//Function body}. Usage: Use the := operator to receive the return value; use the return keyword to return multiple values.

Anonymous functions in PHP8.0 Anonymous functions in PHP8.0 May 14, 2023 am 08:31 AM

PHP8.0 is the latest version of the PHP programming language. One important update is improvements and enhancements to anonymous functions. An anonymous function (also called a closure) is a special type of function that can be created dynamically at runtime and passed to other functions or stored in a variable. In PHP, anonymous functions are crucial for advanced programming and web development. PHP8.0 provides some new syntax and features that make anonymous functions more flexible and easier to use. Some of the updates are as follows: Type declarations for function parameters in PHP8.0,

How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? Oct 24, 2023 am 10:30 AM

How to use PHP7’s anonymous functions and closures to achieve more flexible and reusable code logic? In the world of PHP programming, anonymous functions and closures are very valuable and powerful tools. PHP7 introduces some new language features that make using anonymous functions and closures more convenient and flexible. This article will introduce how to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic, and provide some specific code examples. 1. Anonymous function An anonymous function is a function without a name. In PHP, you can use anonymous

Python Lambda Expressions: Making Programming Easier Python Lambda Expressions: Making Programming Easier Feb 19, 2024 pm 09:54 PM

A python Lambda expression is a small anonymous function that stores an expression in a variable and returns its value. Lambda expressions are often used to perform simple tasks that can be accomplished by writing a separate function, but Lambda expressions can make the code more concise and readable. The syntax of a Lambda expression is as follows: lambdaarguments: expressionarguments is the parameter list received by the Lambda expression, and expression is the body of the Lambda expression, which contains the code that needs to be executed. For example, the following Lambda expression adds two numbers and returns their sum: lambdax,

Python Lambda expressions: abbreviated, concise, powerful Python Lambda expressions: abbreviated, concise, powerful Feb 19, 2024 pm 08:10 PM

pythonLambda expressions are a powerful and flexible tool for creating concise, readable, and easy-to-use code. They are great for quickly creating anonymous functions that can be passed as arguments to other functions or stored in variables. The basic syntax of a Lambda expression is as follows: lambdaarguments:expression For example, the following Lambda expression adds two numbers: lambdax,y:x+y This Lambda expression can be passed to another function as an argument as follows: defsum( x,y):returnx+yresult=sum(lambdax,y:x+y,1,2)In this example

See all articles