What is the best practice for securely passing variables to iframes in PHP?

When passing variables to iframes in PHP, it is important to sanitize and validate the input to prevent any security vulnerabilities such as XSS attacks. One way to securely pass variables to iframes is by using the htmlentities() function to sanitize the input before passing it to the iframe.

<?php
// Sanitize and validate the input
$variable = htmlentities($_GET['variable']);

// Embed the variable in the iframe
echo '<iframe src="iframe.php?variable=' . $variable . '"></iframe>';
?>