How can PHP beginners avoid XSS vulnerabilities when using $_SERVER['PHP_SELF']?

When using $_SERVER['PHP_SELF'] in PHP, beginners can avoid XSS vulnerabilities by properly sanitizing the input before using it in the output. This can be done by using htmlspecialchars() function to encode special characters in the input data.

$form_action = htmlspecialchars($_SERVER['PHP_SELF']);