The php Function HTMLentities
The php function htmlentities turns special characters in a string into their HTML entity equivalents. This can be useful if you're processing user input that may be used in a query or for a URL and that could contain special characters like angle brackets
You'll want to run this function before using a function that will edit the text of your page, such as nl2br(). This will prevent the text from becoming garbled in case your input contains special characters that aren't valid HTML.
In general, you'll want to use htmlentities for anything that will get displayed to a browser or saved in an HTML file for later viewing. This includes things that will be stored in a database, such as names, or values entered by a user in a form.
Another way that you might use htmlentities is to protect against cross-site scripting (XSS). For example, if a user enters a string in your application such as "angle
Note that this is different from the strip_tags() function which strips tags from a string. Both functions serve different purposes, and you'll want to use the right one depending on your application's needs.