Notice: Use of undefined constant ENT_HTML5 - assumed 'ENT_HTML5'

你离开我真会死。 提交于 2019-12-23 07:05:14

问题


I am trying to create a simple method which accepts the parameters for htmlspecialchars. Although I am getting PHP notice:

Use of undefined constant ENT_HTML5 - assumed 'ENT_HTML5'

  1. Any ideas what could be causing this?

/**

 * Encode string.
 *
 * @param array/string $value
 * @param string $param
 * @return string
 */
protected function escape($mixed, $params) {

    $defaults = array('flags' => ENT_QUOTES | ENT_HTML5, 'charset' => 'UTF-8');
    $params = array_merge($defaults, $params);

    if (is_array($mixed)) {
        foreach($mixed as $key => $value) {
            $mixed[$key] = $this->escape($value, $params['flags'], $params['charset']);
        }
    } elseif (is_string($mixed)) {
        $mixed = htmlspecialchars($mixed, $params['flags'], $params['charset']);
    }

    return $mixed;
}
  1. If I change: ENT_QUOTES | ENT_HTML5 into: ENT_QUOTES, I get a different error

Warning: htmlspecialchars() expects parameter 2 to be long, string given

UPDATE

I am using PHP 5.3 so this is the reason for the HTML5 error. If I change ENT_QUOTES | ENT_HTML5 to ENT_COMPAT | ENT_HTML401 I get the same sort of error:

Notice: Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401'


回答1:


ENT_HTML5, ENT_HTML401, and some others were added in PHP version 5.4 according to the manual. For earlier versions those constants are undefined, and PHP will automatically assume that undefined constants are programming "slips" and convert them to strings.



来源:https://stackoverflow.com/questions/11523282/notice-use-of-undefined-constant-ent-html5-assumed-ent-html5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!