note 119003 added to errorfunc.constants

From: Date: Tue, 15 Mar 2016 16:21:10 +0000
Subject: note 119003 added to errorfunc.constants
Groups: php.notes 
Request: Send a blank email to php-notes+get-205169@lists.php.net to get a copy of this message
I use this code to help mimic the default error handler, the only difference is that the levels end up being all caps, which I don't care to fix. You could also get rid of the underscores, but again, I don't care :P Until php starts adding constants starting with E_ that have values overlapping with other E_ constants, this seems to be the shortest way of converting error code integers to strings understandable by meat bags. It will also work with new types, so that's nice. <?php function friendly_error_type($type) { static $levels=null; if ($levels===null) { $levels=[]; foreach (get_defined_constants() as $key=>$value) { if (strpos($key,'E_')!==0) {continue;} $levels[$value]=substr($key,2); } } return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); } echo friendly_error_type(1); #ERROR echo friendly_error_type(2); #WARNING echo friendly_error_type(3); #Error #3 ?> Tested on 5.6.12 and 7.0.3 (The first was by accident, didn't realize I was sshed into production :3) ---- Server IP: 72.52.91.14 Probable Submitter: 75.168.215.187 ---- Manual Page -- http://php.net/manual/en/errorfunc.constants.php Edit -- https://master.php.net/note/edit/119003 Del: integrated -- https://master.php.net/note/delete/119003/integrated Del: useless -- https://master.php.net/note/delete/119003/useless Del: bad code -- https://master.php.net/note/delete/119003/bad+code Del: spam -- https://master.php.net/note/delete/119003/spam Del: non-english -- https://master.php.net/note/delete/119003/non-english Del: in docs -- https://master.php.net/note/delete/119003/in+docs Del: other reasons-- https://master.php.net/note/delete/119003 Reject -- https://master.php.net/note/reject/119003 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205169) next »