Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > a34ed6838d4b29d38abd504392a4a797 > files > 2425

php-manual-es-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>set_error_handler</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Manual de PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Error Handling and Logging Functions"
HREF="ref.errorfunc.html"><LINK
REL="PREVIOUS"
TITLE="restore_error_handler"
HREF="function.restore-error-handler.html"><LINK
REL="NEXT"
TITLE="trigger_error"
HREF="function.trigger-error.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Manual de PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.restore-error-handler.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.trigger-error.html"
ACCESSKEY="N"
>Siguiente</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.set-error-handler"
></A
>set_error_handler</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN17034"
></A
><P
>    (PHP 4 &#62;= 4.0.1)</P
>set_error_handler&nbsp;--&nbsp;
     Sets a user-defined error handler function.  
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN17037"
></A
><H2
>Description</H2
>string <B
CLASS="methodname"
>set_error_handler</B
> ( string error_handler)<BR
></BR
><P
>&#13;     Sets a user function (<TT
CLASS="parameter"
><I
>error_handler</I
></TT
>) to handle
     errors in a script.  Returns the previously defined error handler (if
     any), or <TT
CLASS="constant"
><B
>FALSE</B
></TT
> on error.  This function can be used for defining your own
     way of handling errors during runtime, for example in applications in
     which you need to do cleanup of data/files when a critical error happens,
     or when you need to trigger an error under certain conditions (using
     <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>)
    </P
><P
>&#13;     The user function needs to accept 2 parameters: the error code, and a
     string describing the error. The example below shows the handling of 
     internal execptions by triggering errors and handling them with a user
     defined function:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN17050"
></A
><P
><B
>Ejemplo 1. 
       Error handling with <B
CLASS="function"
>set_error_handler()</B
> and
       <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

// redefine the user error constants - PHP4 only
define (FATAL,E_USER_ERROR);
define (ERROR,E_USER_WARNING);
define (WARNING,E_USER_NOTICE);

// set the error reporting level for this script
error_reporting  (FATAL + ERROR + WARNING);

// error handler function
function myErrorHandler ($errno, $errstr) {
    switch ($errno) {
    case FATAL:
    echo "&#60;b&#62;FATAL&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    echo "  Fatal error in line ".__LINE__." of file ".__FILE__;
    echo ", PHP ".PHP_VERSION." (".PHP_OS.")&#60;br&#62;\n";
    echo "Aborting...&#60;br&#62;\n";
    exit -1;
    break;
    case ERROR:
    echo "&#60;b&#62;ERROR&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    break;
    case WARNING:
    echo "&#60;b&#62;WARNING&#60;/b&#62; [$errno] $errstr&#60;br&#62;\n";
    break;
    default:
    echo "Unkown error type: [$errno] $errstr&#60;br&#62;\n";
    break;
    }
}

// function to test the error handling
function scale_by_log ($vect, $scale) {
    if ( !is_numeric($scale) || $scale &#60;= 0 )
    trigger_error("log(x) for x &#60;= 0 is undefined, you used: scale = $scale", 
      FATAL);
    if (!is_array($vect)) {
    trigger_error("Incorrect input vector, array of values expected", ERROR);
    return null;
    }
    for ($i=0; $i&#60;count($vect); $i++) {
    if (!is_numeric($vect[$i]))
    trigger_error("Value at position $i is not a number, using 0 (zero)", 
      WARNING);
    $temp[$i] = log($scale) * $vect[$i];
    }
    return $temp;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2,3,"foo",5.5,43.3,21.11);
print_r($a);

// now generate second array, generating a warning
echo "----\nvector b - a warning (b = log(PI) * a)\n";
$b = scale_by_log($a, M_PI);
print_r($b);

// this is trouble, we pass a string instead of an array
echo "----\nvector c - an error\n";
$c = scale_by_log("not array",2.3);
var_dump($c);

// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
$d = scale_by_log($a, -2.5);

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     And when you run this sample script, the output will be
     <DIV
CLASS="informalexample"
><A
NAME="AEN17055"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>vector a
Array
(
    [0] =&#62; 2
    [1] =&#62; 3
    [2] =&#62; foo
    [3] =&#62; 5.5
    [4] =&#62; 43.3
    [5] =&#62; 21.11
)
----
vector b - a warning (b = log(PI) * a)
&#60;b&#62;WARNING&#60;/b&#62; [1024] Value at position 2 is not a number, using 0 (zero)&#60;br&#62;
Array
(
    [0] =&#62; 2.2894597716988
    [1] =&#62; 3.4341896575482
    [2] =&#62; 0
    [3] =&#62; 6.2960143721717
    [4] =&#62; 49.566804057279
    [5] =&#62; 24.165247890281
)
----
vector c - an error
&#60;b&#62;ERROR&#60;/b&#62; [512] Incorrect input vector, array of values expected&#60;br&#62;
NULL
----
vector d - fatal error
&#60;b&#62;FATAL&#60;/b&#62; [256] log(x) for x &#60;= 0 is undefined, you used: scale = -2.5&#60;br&#62;
  Fatal error in line 16 of file trigger_error.php, PHP 4.0.1pl2 (Linux)&#60;br&#62;
Aborting...&#60;br&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     See also <A
HREF="function.error-reporting.html"
><B
CLASS="function"
>error_reporting()</B
></A
>,
     <A
HREF="function.restore-error-handler.html"
><B
CLASS="function"
>restore_error_handler()</B
></A
>,
     <A
HREF="function.trigger-error.html"
><B
CLASS="function"
>trigger_error()</B
></A
>, <A
HREF="function.user-error.html"
><B
CLASS="function"
>user_error()</B
></A
>
    </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.restore-error-handler.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Inicio</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.trigger-error.html"
ACCESSKEY="N"
>Siguiente</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>restore_error_handler</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.errorfunc.html"
ACCESSKEY="U"
>Subir</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>trigger_error</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>