Notice: Use of undefined constant ENT_HTML401 – assumed ‘ENT_HTML401’ In … At Line …


When you use function htmlentities

htmlentities($content, ENT_COMPAT | ENT_HTML401, 'UTF-8');

you might get the error:

Notice: <strong>Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401'</strong> In ... At Line ...

We know the usage of this function:

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

To change the encoding, we must set flag, and the default of flag is: ENT_COMPAT | ENT_HTML401

Why the error occur?

Because ENT_HTML401 is only available on PHP 5.4+.

So, we must check php version before use the flag ENT_HTML401

if (version_compare(phpversion(), '5.4', '<')) {
    $flags = ENT_COMPAT; 
}else{
    $flags = ENT_COMPAT | ENT_HTML401; 
}
htmlentities($content, $flags, 'UTF-8');

php htmlentities function

Or you can use:

htmlentities($content, version_compare(phpversion(), '5.4', '<') ? ENT_COMPAT : (ENT_COMPAT | ENT_HTML401), 'UTF-8');

The function works now!

Leave a Reply