


Detailed explanation of PHP error handling in development mode and product mode
Program errors are always inevitable, even though we have been extra careful when writing code.
When developing php programs, we hope that when encountering php errors, they can be displayed to us as soon as possible to facilitate debugging. When the program is developed and becomes a formal product, we hope to record unexpected error messages in the error log instead of displaying these error messages to users, because users are very likely to use these to expose script paths, database information or Other error messages indicate destructive hacking operations.
PHP error handling
If the php script encounters an error during execution, it will be handled in the form of an error. Some errors will terminate the script and continue execution after the error is reported. Some won't, please refer to the manual for details.
php's error handling method is based on the following configuration options. These configurations can be declared in the code or set in the php.ini file. If you do not need to change these configurations frequently, it is recommended to set them in the php.ini file to make your code cleaner and more concise.
# 是否打印错误信息到浏览器/命令行界面 # 开发模式下建议开启,产品模式下强烈建议关闭 ini_set('display_errors', 'On'); # 是否记录错误信息到日志 # 开发模式和产品模式下都建议开启 ini_set('log_errors', 'On'); # 指定错误信息日志文件,若开启了 log_errors 选项,记得指定日志文件位置 # 要确保执行 php 脚本的系统用户拥有该文件的 write 权限,否则日志无法被写入 ini_set('error_log', '/usr/local/php/errors.log'); # 该选项用以设定错误报告的等级 # 等同于 error_reporting(E_ALL) # 无论开发模式还是产品模式下都建议开到E_ALL(报告所有的错误信息) # 产品模式下也需要设置此选项,因为关闭了 display_errors 并开启了 log_errors # 所以浏览器/命令行界面不会因此暴露报错信息 ini_set('error_reporting', E_ALL);
In addition, PHP also provides developers with a built-in function to record customized error information to the error log file in the code:
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
The required parameter is message, Calling this function will write the message to the error_log file defined in php.ini.
User-defined error handling
In addition, users can handle script running in a customized way through the function:
mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )
Errors that occur when the user registers an error_handler and specifies error_types, then when errors of these error_types occur, PHP's standard error handler
will be bypassed (that is, no error message will be output, The error message will not be logged), but the handler in error_handler will be executed.
For detailed usage of this function, please refer to the manual (https://www.php.net/manual/zh/function.set-error-handler.php)
Recommended related article tutorials: php tutorial
The above is the detailed content of Detailed explanation of PHP error handling in development mode and product mode. For more information, please follow other related articles on the PHP Chinese website!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
