How can the code be optimized to prevent empty LI outputs in the navigation function?

The issue of empty LI outputs in the navigation function can be solved by adding a condition to check if the $page['url'] is not empty before outputting the LI element. This will prevent empty LI elements from being added to the navigation menu.

function display_navigation($pages) {
    $output = '<ul>';
    
    foreach ($pages as $page) {
        if (!empty($page['url'])) {
            $output .= '<li><a href="' . $page['url'] . '">' . $page['title'] . '</a></li>';
        }
    }
    
    $output .= '</ul>';
    
    return $output;
}

// Example usage
$pages = array(
    array('title' => 'Home', 'url' => 'index.php'),
    array('title' => 'About', 'url' => ''),
    array('title' => 'Services', 'url' => 'services.php'),
    array('title' => 'Contact', 'url' => 'contact.php')
);

echo display_navigation($pages);