Home > Article > Backend Development > A brief analysis of PHP error handling, automatic loading, stack memory and running mode
The content of this article is to share with you a brief analysis of PHP error handling, automatic loading, stack memory and operating mode. It has certain reference value. Friends in need can refer to it
PhpError handling
PhpError level:
E_ERROR fatal error, which will terminate the script. The value is 1
E_WARNING Warning error, giving a prompt, and will not terminate the operation. The value is 2
E_PARSE Syntax parsing error during compilation. Parsing errors are only generated by the analyzer. The value is 4
E_NOTICE notifies errors during runtime, indicating that the script may encounter errors. The value is 8
E_CORE_ERROR during PHP initialization startup A fatal error occurred during the process. This error is similar to E_ERROR, but is generated by the PHP engine core. The value is 16
E_CORE_WARNING A warning (non-fatal error) that occurred during PHP initialization startup. Similar to E_WARNING, but generated by the PHP engine core. The value is 32
E_COMPILE_ERROR Fatal compile-time error. Similar to E_ERROR, but generated by the Zend script engine. The value is 64
E_COMPILE_WARNING compile-time warning (non-fatal error). Similar to E_WARNING, but generated by the Zend scripting engine. The value is 128
E_USER_ERROR User-generated error message. Similar to E_ERROR, but is generated by the user using the PHP function trigger_error() in the code. The value is 256
E_USER_WARNING Warning message generated by the user. Similar to E_WARNING, but is generated by the user using the PHP function trigger_error() in the code. The value is 512
E_USER_NOTICE Notification information generated by the user. Similar to E_NOTICE, but is generated by the user using the PHP function trigger_error() in the code. A value of 1024
E_STRICT enables PHP's suggestions for code modifications to ensure the best interoperability and forward compatibility of the code. The value is 2048
E_RECOVERABLE_ERROR, a fatal error that can be captured. It indicates that a potentially dangerous error has occurred, but has not caused the PHP engine to become unstable. If the error is not caught by a user-defined handler (see set_error_handler()), it will become an E_ERROR and the script will terminate. The value is 4096
E_DEPRECATED runtime notification. When enabled, a warning will be given about code that may not work properly in future versions. The value is 8192
E_USER_DEPRECATED User-generated warning message. Similar to E_DEPRECATED, but is generated by the user using the PHP function trigger_error() in the code. The value 16384
E_ALL represents all error and warning information except E_STRICT. The value is 30719
Use bit operator combinations to display or mask errors (binary permission judgment)
PhpAbout error configuration
error_reporting Set the level of error reporting, The level setting can be seen above
The default value is E_ALL & ~E_NOTICE, which means that all errors except E_NOTICE and E_STRICT are displayed
The E_STRICT error level is not included in E_ALL. You must explicitly enable this level to appear.
There is no error level constant used outside of PHP Meaningful, you can use decimal numbers instead, for example, 2147483647 includes all errors
display_errors Whether to output errors to the screen
Although you can use ini_set to reset it, it cannot be set when a fatal error occurs in php
display_startup_errors Whether to display errors during startup
log_errors Whether to record the error information of the script tolog
log_errors_max_len
Set the maximum number of bytes in log_errors. Information about the error source will be added to error_log. The default value is 1024. If set to 0, there is no limit to the length. This length setting limits logged errors, displayed errors, and $php_errormsg.
ignore_repeated_errors
Do not record repeated error messages,
ignore_repeated_source
When ignoring repeated messages, the source of the message is also ignored. When this setting is on, duplicate messages will not record whether they were generated by different files or different lines of source code.
report_memleaks
If this parameter is set to Off, the memory leak information will not be displayed (in stdout or log middle). This report will be sent to stderr on Posix platforms. On Windows, it will be sent to the debugger using OutputDebugString(), and can be viewed with tools like » DbgView. This is only effective for debugging compilation, and error_reporting needs to contain E_WARNING to work
##track_errors
If enabled , the last error will always exist in the variable $php_errormsg.
html_errors
##Close the HTML tag in the error message. This new HTML-formatted error message is clickable and directs the user to a reference page that describes the error or the function that caused the error. These references relate to the settings of docref_root and docref_ext.
error_prepend_string string
The content output before the error message.
error_append_string string
The content output after the error message.
error_log
Set the file to which script errors will be logged. The file must be writable by the web server user. If the special value syslog is set, error messages are sent to the system logger. On Unix and similar systems, syslog(3) is used, and on Windows NT-like systems, the event log is used. System logging is not supported on Windows 95. See: syslog(). If this configuration is not set, error messages will be sent to the SAPI error logger. For example, appear in Apache's error log, or be sent to stderr in the CLI.
Error handling related methods and personal understanding of usage
debug_backtrace - Generate a backtrace (backtrace). Parameters can be set to limit the number of returned stacks.can find out the stack information of calling the function, which is very helpful for troubleshooting, and tp's Debug is similar to
##debug_print_backtrace(); prints the traceback directly, similar to debug_backtrace,
##error_clear_last - clears the latest error
error_get_last — Get the last error that occurred
error_log — Send the error message to a certain place, you can save the error into a file, but the error message cannot be null
error_reporting — Set what kind of PHP errors should be reported, the same as php.ini
restore_error_handler — Restore the previous error handling function,
restore_exception_handler — Restore the previously defined exception handling function.
set_error_handler — Set user-defined error handling function, which needs to be defined before the error
The following level of errors cannot be defined by the user function to handle: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most E_STRICTs generated in the file where the set_error_handler() function is called.
Just like the ini setting of error_reporting can control the display of errors, the second parameter can be used to block the triggering of error_handler. Without this mask, error_handler will be called on every error regardless of how error_reporting is set.
set_exception_handler — Set user-defined exception handling function
trigger_error — Generate a user-level error/warning/notice message
user_error — Alias for trigger_error
register_shutdown_function The registration function executed after the PHP termination script, the first parameter supports the function, and a statement containing the instantiation class, an array of class methods (the class will be instantiated first when registering); (As long as the registration is successful, Any error can be captured)
##Phpautomatically Loading
Personal opinion
The main difference between spl_autoload_register and __autoload is
__autoload It's just that a function can only be defined once in php. If you want to load plug-ins, etc., you need to constantly judge if else or composer, which will be very troublesome.
spl_autoload_register can be based on the folder, Or plug-in, customize various processing functions, create an automatically loaded queue, and continue to search according to the queue; until the queue is completed or returns true (the default return of true when a file is found)
Stack memory(Personal understanding)
Heap: Stores user-defined variables
Stack: Some basic type variables defined in the function and object reference variables are all in the stack space. When they exceed the scope, they will be Will be released automatically
Information supplement:
Heap:
When the variables defined in the file are modified statically, they will be changed to the global data area and will not occupy the stack memory
Stack:
The stack memory generally stores the function call information and the variables declared in the function. Because the function call is recursive, the outer function must be called before the inner function The function is loaded and executed first, and it must wait until the inner called function is completed before it can end. This first-in-last-out mechanism is why it is called stack memory.
PS: When compiling, the compiler will first collect all the variables defined in this function and put them at the front of the function to apply for memory, so the order in which they enter and exit the stack is not what you are writing. The order of the program is defined, but the stack is pushed forward when the function is executed, and popped off the stack after the function execution is completed.
Others:
##Const, global, and static are all stored in global data after modification AreaSuper global variables and global variables are all static variables and are stored in the global data area
There is less information and is waiting for correction and improvement
PhpwebRunning Mode
PhpRunning Mode:
1) CGI (Common Gateway Interface/Common Gateway Interface)Generally, executable programs, such as EXE files, and WEB servers occupy different processes, and generally a CGI program can only handle one user request. In this way, when the number of user requests is very large, it will occupy a large amount of system resources, such as memory, CPU time, etc., resulting in low performance.
FastCGI is a scalable, high-speed communication interface between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP.
FastCGI interface mode adopts C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server at the same time. Every time the HTTP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to exclusively process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
Php-fpm is the fastcgi manager that comes with php
3) CLI (Command Line Run/Command Line Interface) 4.Web module mode (the mode in which web servers such as Apache run) This mode is an extension of apache based on cgi
5.ISAPI (Internet Server Application Program Interface) is a set of API interfaces for WEB services provided by Microsoft. It can realize the functions provided by CGI. All functions have been extended on this basis, such as providing a filter application interface. ISAPI applications are mostly used in the form of DLL dynamic libraries, which can be executed after being requested by the user. They will not disappear immediately after processing a user request, but will continue to reside in the memory and wait for other user inputs to be processed. In addition, the ISAPI DLL application and the WEB server are in the same process, and the efficiency is significantly higher than that of CGI.
##Php2kind andwebServer interaction: ##Nginx: The user initiates a request and uses nginx's current handshake. When nginx receives it, it is pushed to php-fpm for processing. When php-fpm is busy, nginx will return 504 getway Apache: Apache has 3 operating modes, prefork, worker, Event, Create different processes according to different modes When processes and threads receive relevant php, they are handed over to the apache module for processing
The above is the detailed content of A brief analysis of PHP error handling, automatic loading, stack memory and running mode. For more information, please follow other related articles on the PHP Chinese website!