3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ErrorPrinter { /* ARRAY KEYS */ private $KEY_NUMBER_OF_ERRORS = "number_of_errors"; //key for number of errors present private $KEY_ERRORS = "errors"; //key for the array of errors /* ERROR CODES */ const ERROR_MISSING_PARAM = "10000"; //parameter missing from http post private $response = array(); private $errors = array(); private $number_of_errors = 0; function addError($error, $variable){ $this->errors[$variable] = $error; $this->number_of_errors = count($this->errors); } function printToJson(){ $response[$this->KEY_ERRORS] = $this->errors; $response[$this->KEY_NUMBER_OF_ERRORS] = $this->number_of_errors; return json_encode($this->response); } function getNumberOfErrors(){ return $this->number_of_errors; } } $printer = new ErrorPrinter; $printer->addError(ErrorPrinter::ERROR_MISSING_PARAM, 'name'); echo $printer->printToJson();

preferences:
46.48 ms | 402 KiB | 5 Q