How can PHP developers properly replace "&" with "&" in a string?

When outputting HTML in PHP, it is important to properly encode special characters like "&" to prevent parsing errors and ensure proper rendering. To replace "&" with "&" in a string, you can use the PHP function `htmlspecialchars()` which converts special characters to HTML entities. This function will replace "&" with "&" and other special characters as needed.

$string = "This is a string with & symbol";
$encoded_string = htmlspecialchars($string, ENT_QUOTES);
echo $encoded_string;