title: How to set http response status code and message in php
body: <p>I have page which checks data sent via HTTP POST (<a href="http://example.com/http_post" rel="noreferrer">http://example.com/http_post</a>). If the data is good I add to database and wish to set http response code 201 and a success message as http response. If not I collect the errors in a an array and wish to set http response code and message as serialized JSON array as http response. </p>
  
  <p>1) Whats the syntax to serialze the errors array as JSON in php?</p>
  
  <p>Example</p>
  
  <pre><code>{
    "message": "The request is invalid.",
      "modelState": {
        "JobType": [ "Please provide a valid job type eg. Perm"]
    }
  }
  </code></pre>
  
  <ol start="2">
  <li><p>Whats the syntax to set and return the http response to 412.</p></li>
  <li><p>Whats the syntax to set and return the serialized JSON in the http response body as above.</p></li>
  </ol>
  
  <p>An example would be helpful on how to set all these http response headers.</p>
  
  <p>Thanks</p>