<p>I'm writing some RSS feeds in PHP and stuggling with character-encoding issues. Should I utf8_encode() before or after htmlentities() encoding? For example, I've got both ampersands and Chinese characters in a description element, and I'm not sure which of these is proper:</p>

<pre><code>$output = utf8_encode(htmlentities($source)); or
$output = htmlentities(utf8_encode($source));
</code></pre>

<p>And why?</p>

You want to do
```
$output = htmlentities(utf8_encode($source));
```
.  This is because you want to convert your international characters into proper UTF8 first, and then have ampersands (and possibly some of the UTF-8 characters as well) turned in to HTML entities.  If you do the entities first, then some of the international characters may not be handled properly.
If none of your international characters are going to be changed by utf8_encode, then it doesn't matter which order you call them in.