What is the best PHP function to convert special characters in a string?

When dealing with special characters in a string, it is important to convert them to their corresponding HTML entities to ensure proper display and prevent any potential security vulnerabilities. The PHP function `htmlspecialchars()` is commonly used for this purpose as it converts special characters to their HTML entity equivalents.

$string = "This is a string with special characters like < and >";
$encoded_string = htmlspecialchars($string, ENT_QUOTES);
echo $encoded_string;