PHP htmlentities

When I use htmlentities() function in my PHP projects, should I always use it with a flag so it is compatible with other language characters?

$my_variable = $some_data;
$output_variable = htmlentities($my_variable);

or...

$my_variable = $some_data;
$output_variable = htmlentities($my_variable, ENT_COMPAT, 'UTF-8');

If neither of the above, what is the proper way to use this function.

Thank you!

If you're unsure what charset you're using, the second way it the better. However, for most occasions, you'll find that the first one is fine.

Generally speaking, you shouldn't use it at all. Specifying the encoding used in the Content-Type HTTP header and then using real characters instead of entities is generally more efficient. (OTOH, you should use htmlspecialchars to convert characters which have special meaning in HTML to entities).

If you do use it, then you need to specify what encoding you are converting from if you aren't using the default (ISO-8859-1). Specifying UTF-8 when you aren't using UTF-8 is less than helpful.