0

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!

5
  • Try this : <meta charset="UTF-8" /> Commented Aug 24, 2018 at 14:38
  • $errormessage = $errormessage + $extra + $error + "."; should use .s not +s. Commented Aug 24, 2018 at 14:38
  • That error sounds like it comes from WebPack DevServer ... it's not a standard PHP error message. I'm guessing you've got a PHP error, with display errors on, using that DevServer ... and since PHP will spaff out the error message as an HTML snippet (by default) you're getting that encoding message from the DevServer. Commented Aug 24, 2018 at 14:41
  • ensure that the header is set before you write anything into the html buffer. (eg: pure html or echo), ensure that your apache server doesn't change the header (by looking in http header (in chrome F12 and Network tab and refresh)) Commented Aug 24, 2018 at 14:42
  • You should do two important things. Use an IDE that will highlight mistakes and Turn on your PHP display error. If you're new to php, most developers have these two things covered. =) Commented Aug 24, 2018 at 14:46

1 Answer 1

3

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 ;
Sign up to request clarification or add additional context in comments.

3 Comments

Using a proper code editor with syntax highlighting and linter would certainly help you out too.
yeah, most definitely!
The program I use to code in usually fills in the ; and so i have gotten into lazy habits. Just to make sure I put them in and it worked so thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.