Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Right, this is a bizarre solution, but I <em>think</em> it will suit your purposes.</p> <p>After a bit of playing around, I have discovered that this:</p> <pre><code>function my_error_handler ($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) { // Handle the error here @trigger_error($errstr); return TRUE; } // Just to make sure PHP is not outputting anything error_reporting(-1); ini_set('display_errors',1); set_error_handler('my_error_handler'); // An E_USR error... trigger_error('Some error'); var_dump(error_get_last()); // ...and a native one $key = count(); var_dump(error_get_last()); </code></pre> <p>Results in this:</p> <pre><code>array(4) { ["type"]=&gt; int(1024) ["message"]=&gt; string(10) "Some error" ["file"]=&gt; string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php" ["line"]=&gt; int(7) } array(4) { ["type"]=&gt; int(1024) ["message"]=&gt; string(45) "count() expects at least 1 parameter, 0 given" ["file"]=&gt; string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php" ["line"]=&gt; int(7) } </code></pre> <p>Calling <code>@trigger_error()</code> from within your error handler, and not returning <code>FALSE</code>, causes <code>error_get_last()</code> to return something other than <code>NULL</code>, but because the error is suppressed with <code>@</code>, PHP doesn't output anything. It seems that in the interests of avoiding an infinite recursion, calling <code>trigger_error()</code> from within the registered error handler function does not call the error handler - which works to our advantage here.</p> <p>Obviously, the error code has been modified, but you could convert it to the relevant <code>E_USR_*</code> code if you need - but I suspect what you really want is the string value, which this method will allow you get. You have, unfortunately, also lost the line number and file information - although you could possibly get this back by doing something involving a stack trace inside the error handler, or at the very least including it in the string from the arguments passed.</p> <p>This is a horrible, horrible, horrible hack - but since there is no officially sanctioned way to do this, a hack is essentially what you are asking for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload