What are the best practices for handling variable passing in PHP to avoid link and image display issues?

When passing variables in PHP, it is important to properly escape and sanitize the data to prevent any link or image display issues. This can be done by using functions like htmlspecialchars() to encode special characters and prevent XSS attacks. Additionally, it is good practice to validate and filter input data before displaying it to ensure it meets the expected format.

// Example of passing a variable safely in PHP
$unsafe_variable = "<script>alert('XSS attack!')</script>";
$safe_variable = htmlspecialchars($unsafe_variable);

echo $safe_variable;