What are the best practices for making invalid links clickable in PHP?

When dealing with invalid links in PHP, it is important to handle them gracefully by making them clickable without causing errors. One way to achieve this is by checking if the link is valid using the filter_var function with the FILTER_VALIDATE_URL flag. If the link is valid, it can be displayed as a clickable hyperlink using the anchor tag.

$link = "invalid_link";

if (filter_var($link, FILTER_VALIDATE_URL)) {
    echo '<a href="' . $link . '">Click here</a>';
} else {
    echo 'Invalid link: ' . $link;
}