XSS attacks, SQL injection attacks and many related problems occur when arbitrary values are evaluated in a specific context according to certain rules, and not enough care is applied to make sure the input values do not trigger unwanted rules and side effects.
What does that mean in concrete terms? For example:
<script src="foo/bar.js"></script>
This snippet of text triggers certain actions when evaluated as HTML. It does not do anything when you evaluate it as plain text, or when you store it in a database, or when you print it onto a piece of paper.
If one of your users submits this kind of value in a form, it's just text and doesn't hurt anyone. It will only start to "do" something if you put this into HTML and let the browser evaluate it. That is an XSS attack.
Just doing if (isset($_POST['answer']))
does not do anything with the text as such. It does not evaluate the text's value. All it does is check for the existence of an array index. You can even safely do substr($_POST['answer'], 10)
or any other possible text operation, nothing will parse and evaluate the string with HTML rules.
When you do need to embed text in text and have it evaluated, such as dynamically creating HTML output or SQL queries, you need to ensure arbitrary user input is properly escaped so as not to trigger unwanted special rules. (Or other equivalent methods to prevent accidental evaluation of the text, such as parameterised queries in case of SQL.) See for example How can I prevent SQL injection in PHP? and The Great Escapism (Or: What You Need To Know To Work With Text Within Text) for in depth details.