-2

If i was to have an array of error codes I.E. $error_code =[400=> "Bad request", 404 => "Not found"];.

How would i create a function that accepts 2 input parameters error_code & error_message. And prints out a header that has the form: Protocol Code - Reason(custom message dependant on what went wrong).

I.E. "error": 400 - Bad Request: Name contains whitespaces

Roger
  • 3
  • 3
  • it's unclear where you are stuck. The simple answer is `function showError($code, $message) { // do your formatting and output }` – Jeff Sep 30 '18 at 15:12
  • i'm not sure what to put in the formatting. – Roger Sep 30 '18 at 15:12
  • Possible duplicate of [PHP: How to send HTTP response code?](https://stackoverflow.com/questions/3258634/php-how-to-send-http-response-code) – splash58 Sep 30 '18 at 15:14
  • 1
    StackOverflow isn't your personal coding factory. Try to learn some PHP. – Sakura Kinomoto Sep 30 '18 at 15:15
  • I'm not really sure thats a duplicate. I'm trying to use a function to output not just use header and if statements – Roger Sep 30 '18 at 15:17

1 Answers1

1

It is not possible to use header(); for something like that, but you can echo/return the function.

As far as I understand this should be the solution:

function getError($error_code, $error_message) {
echo "error: ".$error_code." - ".$error_message;
}

Please note that you will need to format the error message if you want to get the result you mentioned in your question.

Radek
  • 63
  • 9