Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common errors and exception handling methods in php

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "common errors and exception handling methods in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php is not like other computer programming languages that throw an exception when it encounters an error. PHP also has an exception mechanism when dealing with objects, but PHP will execute it as happily as possible, ignoring what happens, and will not throw an exception unless it encounters an extremely serious error. The main purpose of this article is to share the error exception handling mechanism related to PHP.

Error level

PHP has several error severity levels. The three most common types of information are error, notice, and warning. They have different seriousness: E_ERROR, E_NOTICE, and E_WARNING. Errors are a serious problem at run time, usually caused by an error in the code, and must be fixed, otherwise PHP execution will stop. Notifications are recommended information because the program code may cause problems during execution, but the program will not stop. The warning is a non-fatal error and the execution of the program will not be suspended.

Using PHP's built-in function error_reporting (), you can set the error level during program execution by passing in a predefined error level constant, which means that if you only want to see warnings and errors-not notifications-you can do this:

Error_reporting (E_ERROR | E_WARNING)

You can have PHP use the error control operator @ to suppress specific errors, such as @ fopen (). If you place this operator before the expression, no subsequent errors will occur. But I don't recommend it.

Error report

The error log is very helpful in finding errors in the program, but sometimes it exposes the structure of the application to the outside world. In order to effectively protect your application from the problems caused by it.

In a development environment, I like to have PHP display and log all error messages, while in a production environment, I have PHP log most error messages but not show them. No matter what you do, be sure to follow these four rules:

Be sure to have PHP report errors.

Displays errors in the development environment.

Errors cannot be displayed in a production environment.

Errors are recorded in both the development environment and the production environment.

I set error reporting for the development environment in php.ini as follows:

; display error

Display_errors = On

Display_startup_errors = On

; report all errors

Error_reporting =-1

; record errors

Log_errors = On

I set up error reporting for the production environment in php.ini as follows:

; do not display errors

Display_errors = Off

Display_startup_errors = Off

Report all other errors except for precautions

Error_reporting = E_ALL & ~ E_NOTICE

; record errors

Log_errors = On

Exception capture

Exceptions are standard in many popular programming languages, but they are often ignored by PHP developers. For example, Ruby is a language that attaches great importance to exceptions, no matter what errors occur, such as a failed HTTP request, or a problem with a database query, or even can't find a picture resource, Ruby (or the gems used) will throw an exception, and you can immediately know what's going on on the screen.

PHP handles this problem more casually, and calling the file_get_contents () function usually gives only FALSE values and warnings. Many older PHP frameworks such as CodeIgniter simply return false, write the information to a proprietary log, or let you use methods like $this- > upload- > get_error () to see the cause of the error. The problem here is that you have to find out what the error is and flip through the documentation to see what wrong methods the class uses instead of explicitly exposing the error.

Another problem occurs when the class automatically throws an error to the screen to end the program. Doing so will prevent other developers from dealing with errors dynamically. Exceptions should be thrown to make developers aware of errors and allow them to choose how to handle them.

This is the end of the content of "Common errors and exception handling in php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report