How can PHP functions be utilized to improve readability and maintainability of code for formatting HTML elements?

When working with HTML elements in PHP, it can become messy and difficult to maintain the code if the HTML is directly embedded within the PHP script. To improve readability and maintainability, PHP functions can be utilized to encapsulate the HTML formatting into separate functions. This way, the HTML structure can be easily modified or updated without affecting the PHP logic.

<?php

// Function to create a formatted HTML link
function createLink($url, $text) {
    return "<a href='$url'>$text</a>";
}

// Function to create a formatted HTML image
function createImage($src, $alt) {
    return "<img src='$src' alt='$alt'>";
}

// Example usage
$link = createLink("https://www.example.com", "Click here");
$image = createImage("image.jpg", "Example Image");

echo $link;
echo $image;

?>