PHP file character encoding not declared error

799

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!

444

Answer

Solution:

You have multiple PHP errors. E.G 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;

People are also looking for solutions to the problem: php - Cookie (un)serialization in Laravel 5.5.42

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.