Why can I not change the HTTP status code on PHP?

I’m trying to return HTTP 400 (Bad Request) and it seems like it’s not working. It just returns 200 (OK)
Here’s the code:

<html>
  <body>
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
      http_response_code(400);
      echo http_response_code();
    }
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $data = json_decode(file_get_contents('php://input'), true);
    }
    ?> 
  </body>
</html>

When I open the web page, it shows “400” because of echo. However, if I go into the Google developer console, it says “Status Code: 200 OK”
How can I send an HTTP status code?

Hello @hotrodrose4, welcome!

You can try using the below code:

<html>
  <body>
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
      http_response_code(400);
      echo http_response_code();
      exit;
    }
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $data = json_decode(file_get_contents('php://input'), true);
    }
    ?> 
  </body>
</html>

Thank you for the help, but that doesn’t seem to fix it. It still returns 200 OK on both the Google developer console and an API checker (https://reqbin.com/)
Does Replit send 200 after the code sends 400?

Hello, I traced the error to the html and body tags. Here’s the fixed code:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  http_response_code(400);
  echo http_response_code();
  exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'), true);
}
?>