简体   繁体   English

PHP文件字符编码未声明错误

[英]PHP file character encoding not declared error

I have a PHP file which, when run produces a white screen and this error: "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol." 我有一个PHP文件,该文件在运行时会产生白屏,并且出现以下错误:“未声明HTML文档的字符编码。如果文档包含来自美国以外的字符,则在某些浏览器配置中,文档将呈现乱码ASCII范围。必须在文档或传输协议中声明页面的字符编码。” Looking at other answers on this site the most common answer is to add 在该网站上查看其他答案,最常见的答案是添加

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

in the <head> . <head> However I have this and am still getting the error. 但是,我有这个,仍然出现错误。 I have also tried 我也尝试过

header('Content-type: text/html; charset=utf-8');

in the PHP and it still doesnt work. 在PHP中,它仍然无法正常工作。 When I remove all my php code it works, anyone know what I might have in my code to cause this error? 当我删除所有的php代码时,任何人都知道我的代码中可能包含什么导致此错误? Im a bit of a novice to PHP and still dont really know what im doing. 我对PHP有点新手,但仍然不知道我在做什么。 my full php code is so: 我完整的php代码是这样的:

$favisit = $favanimal = $age = $area = "";
$viserror = $anierror = $ageerror = $areaerror = "";
$errormessage = $extra = "";
$errorlist = [$viserror,$anierror,$ageerror,$areaerror]
$counter = 0;

if ($_SERVER["REQUEST_METHOD"]=="POST") {
    if (empty($_POST["favisit"])) {
        $viserror = " the favorite bit of your visit";
    } else {
        $favisit = test_input($_POST["favisit"]);
    }
    if (empty($_POST["favanimal"])) {
        $anierror = " your favorite animal";
    } else {
        $favanimal = test_input($_POST["favanimal"]);
    }
    if (empty($_POST["age"])) {
        $ageerror = " your age";
    } else {
        $age = test_input($_POST["age"]);
    }
    if (empty($_POST["area"])) {
        $areaerror = " the area where you live";
    } else {
        $area = test_input($_POST["area"]);
    }
}

foreach($errormessage as $error) {
    if ($error == "") {
        $counter = $counter+1
    }
}
if ($counter != 0) {
    $errormessage = "You have not filled out"
    if ($counter == 1) {
        foreach($errormessage as $error) {
            if ($error == "") {
                $errormessage = $errormessage . $error . ".";
            }
        }
    } else {
        $extra = ","
        foreach($errormessage as $error) {
            if ($error == "") {
                if ($counter == 1) {
                    $extra = " or"
                }
                $errormessage = $errormessage + $extra + $error + ".";
                $counter = $counter -1;
            }
        }
    }
}

Again I'm still a novice to PHP so its probably a stupid mistake I cant pick up on. 同样,我还是PHP的新手,所以它可能是我无法理解的愚蠢错误。 Thanks in advance! 提前致谢!

You have multiple PHP errors. 您有多个PHP错误。 EG missing ; EG缺失; on several lines. 在几行上。 So your request probably returns a 500, and you're getting an error in your error log. 因此,您的请求可能返回500,并且您的错误日志中出现错误。

This means you are getting an empty body (and obviously that does not have any meta declaration in it. The error is true but misleading) 这意味着您得到的是一个空的主体(并且显然其中没有任何元声明。该错误是正确的但具有误导性)

you can probably benifit from How do I get PHP errors to display? 您可能会从“ 如何显示PHP错误”中受益?

examples: 例子:

  • $counter = $counter+1 needs a ; $counter = $counter+1需要一个;
  • $errormessage = "You have not filled out" needs a ; $errormessage = "You have not filled out"需要一个;
  • $errormessage = $errormessage + $extra + $error + "."; should be concatenated with . 应该与串联. , not + ,而不是+
  • $extra = "," needs a ; $extra = ","需要一个;
  • $extra = " or" needs a ; $extra = " or"需要一个;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM