如何将PHP函数转换为非函数?

saw*_*awu 0 php refactoring

如何将下面的代码中的PHP函数转换为非函数.

<?php
require_once ('./mysqli_connect.php'); // Connect to the db.

function make_list ($parent)
{
    global $tasks;
    echo '<ol>';
    foreach ($parent as $task_id => $todo)
    {
        echo "<li>$todo";
        if (isset($tasks[$task_id]))
        { 
            make_list($tasks[$task_id]);
        }
        echo '</li>';
    }

    // Close the ordered list:
    echo '</ol>';
}

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT task_id, parent_id, task FROM tasks WHERE date_completed='0000-00-00 00:00:00' ORDER BY parent_id, date_added ASC");

if (!$dbc)
{
    // There was an error...do something about it here...
    print mysqli_error();
} 
$tasks = array();

while (list($task_id, $parent_id, $task) = mysqli_fetch_array($dbc, MYSQLI_NUM))
{
    // Add to the array:
    $tasks[$parent_id][$task_id] =  $task;
}

make_list($tasks[0]);

mysqli_close(); // close the connection

// Include the html footer
include('./includes/footer.html');
?>
Run Code Online (Sandbox Code Playgroud)

如果我没有发布的其他代码是非函数形式,那么保留我的代码会更好吗?

ere*_*non 8

第一:您不能以简单的方式将递归函数转换为意大利面条代码.
第二:没有必要这样做.将您的逻辑,操作分离为函数,以及逻辑和数据表示.不要使用html标记垃圾邮件代码.使用某种模板机制.