?
Solved

Error Handling

Posted on 2006-11-22
10
Medium Priority
?
197 Views
Last Modified: 2008-03-06
In PHP5 if you return false out of the function referenced by set_error_handler the default PHP error handler takes over and handles everything. Is there a way to do this in PHP4? Or perhaps anyone has the default error handler code written in PHP?
0
Comment
Question by:thepadders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:ch2
ID: 18001131
Take a read here.

5.0.0      The error_types parameter was introduced.
4.3.0      Instead of a function name, an array containing an object reference and a method name can also be supplied as the error_handler.
4.0.2      Three optional parameters for the error_handler user function was introduced. These are the filename, the line number, and the context.

http://en.php.net/set_error_handler


The above link contains also an example to handle errors with the set_error_handler.

Hope it helps.
0
 

Author Comment

by:thepadders
ID: 18001158
Yes, I understand all that but it dosen't really help answer my question unfortunatly.
0
 
LVL 11

Expert Comment

by:ch2
ID: 18001214
I don't understand your question. My bad english, sorry. Some one will answer.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:hernst42
ID: 18001355
No if you use a error-handler in php4 the returnvalue of your function is not evaluated by php4 and thus you need to take care of it in your error-handler (e.g. print out the error-message)
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18002243
The whole point of set_error_handler is to offer a way for you to handle errors why would you set a return value?

As an Example :
=========================================================================================

function errMsg ($err_type, $err_msg, $err_file, $err_line) {

      $errortype = array (
                1   =>  "Error",
                2   =>  "Warning",
                4   =>  "Parsing Error",
                8   =>  "Notice",
                16  =>  "Core Error",
                32  =>  "Core Warning",
                64  =>  "Compile Error",
                128 =>  "Compile Warning",
                256 =>  "User Error",
                512 =>  "User Warning",
                1024=>  "User Notice",
                2048=>  "Strict Error"
        );

        if ($err_type <= error_reporting()) {

              // We Need to Handle this Error
              echo      ("      A $errortype[$err_type] has occured in <b>$err_file</b> at line <b>$err_line</b> <br />
                          $err_msg
                    ");
              
              // Log Error
            $error_text            =            "ERROR DATE TIME : ".date("d/m/Y g:m A")."\n";
            $error_text            .=            "ERROR TYPE WAS : $error_name\n";
            $error_text            .=            "ERROR OCCURED AT : $err_file - $err_line\n";
            $error_text            .=            "ERROR WAS : $err_msg\n\n";

            error_log($error_text,3,"c:/temp/errorlog.txt");
        }
}


set_error_handler("errMsg");      // Set Error Handler

=========================================================================================
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18002409
You set an return value, so the default php-error-handler can be used (e.g. you only want to catch errors from cetrain files by your own) As php has some more options to handle errors (e.g. write to webserver-log) this can be a very usefull option.
0
 

Author Comment

by:thepadders
ID: 18005054
Yes, hernst42 understands the reason; I guess there is no easy work around to do this, will just have to wrap the whole thing in a check for PHP5.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 18006594
We also use this method to cope and log certain application errors. before we moved completly to php we hat the following code which worked both with php4 and php5:

if (substr(phpversion(),0,1) < 5) {
    // emulate error-output in php4
    echo '<b> ' . $errortype[$errno] . "</b>: $errmsg in <b>$filename</b> on line <b>$linenum</b><br/>\n";
}

// If debugging is done also render the errors into the page (php5) thing
return false;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question