What best practices should be followed when incorporating external content, such as iframes, in PHP code to avoid errors and security vulnerabilities?

When incorporating external content, such as iframes, in PHP code, it is important to sanitize the input to prevent XSS attacks and other security vulnerabilities. One way to do this is by using the `htmlspecialchars()` function to encode any user input before outputting it to the page. Additionally, you should validate the URLs of any external content to ensure they are safe to include.

// Sanitize user input before outputting it
$userInput = "<iframe src='https://example.com'></iframe>";
echo htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');

// Validate the URL of external content
$externalURL = "https://example.com";
if (filter_var($externalURL, FILTER_VALIDATE_URL)) {
    echo "<iframe src='" . htmlspecialchars($externalURL, ENT_QUOTES, 'UTF-8') . "'></iframe>";
} else {
    echo "Invalid URL";
}