简体   繁体   中英

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." 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> . 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. When I remove all my php code it works, anyone know what I might have in my code to cause this error? Im a bit of a novice to PHP and still dont really know what im doing. my full php code is so:

$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. Thanks in advance!

You have multiple PHP errors. EG missing ; on several lines. So your request probably returns a 500, and you're getting an error in your error log.

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?

examples:

  • $counter = $counter+1 needs a ;
  • $errormessage = "You have not filled out" needs a ;
  • $errormessage = $errormessage + $extra + $error + "."; should be concatenated with . , not +
  • $extra = "," needs a ;
  • $extra = " or" needs a ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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