If your data is going into the database, you must prevent users from sending data that will escape out of your normal query to break or modify that query. Using MySQLi with parameter binding is the current best way to protect your database. So long as you also use a string or preg replace as well. So that an integer ID is ensured to just be an int. You want to remove as many characters as you logically can from your data.
If your data is going to be placed in-between tags, strip_tags followed by htmlentities is a good practice. However, if you can use a white list, do. If you know the output should only be an int, make sure it is. echo htmlentities(strip_tags($value));
If you are placing that data inside a tag <a href='<?php echo $value; ?>'>
the above is not enough. You must also replace single quotes. Here, it is even more vital that you prevent poisoned data from making it in. If $value was something like ' onClick='alert("oops");'
you might be in a serious bind. Here you should absolutely use a white list, and only allow user entered content to appear here if you absolutely must.
If you want to output into a JavaScript. Don't. You shouldn't do this even if you've been working with security for years. However. The one exception can be made for VERY specific values. Integers. Strings that are locked to A-Za-z0-9 and space with a preg replace. However. This is VERY dangerous and can seriously compromise your system.
If you want to put variables inside an eval or exec. JUST DON'T. You are not smart enough to prevent an attack. Neither am I. No one is. It is just a matter of time before someone finds a way. And if the code as it was first written was fine? At some later date the code will get changed and won't be fine anymore. Just don't do it. Period. Or one day you'll be sitting in prison wondering to yourself, "But I didn't hack into the pentagon..."