In the course "Brothers New PHP Video Tutorial", PHP (foreign name: PHP: Hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a general open source scripting language. The syntax absorbs the characteristics of C language, Java and Perl, which is easy to learn and widely used. It is mainly suitable for the field of Web development. PHP's unique syntax mixes C, Java, Perl, and PHP's own syntax. It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, dynamic pages made with PHP embed programs into HTML (an application under the Standard Universal Markup Language) document for execution, and the execution efficiency is much higher than CGI that completely generates HTML tags; PHP can also execute compiled code. Compilation can achieve encryption and optimize code running, making the code run faster.
Course playback address: http://www.php.cn/course/358.html
The teacher’s teaching style:
The teacher’s lectures are simple, clear, layer-by-layer analysis, interlocking, rigorous argumentation, rigorous structure, and use the logical power of thinking to attract students’ attention Strength, use reason to control the classroom teaching process. The teaching skills are full of wit. Various teaching methods and techniques are readily available and can be used freely and appropriately without any trace of polishing.
The more difficult part in this video should be: PHP exception handling:
Exception handling (also known as error The handling) function provides methods for handling errors or exceptions that occur while the program is running.
Exception handling is usually a measure taken to prevent unknown errors from occurring. The advantage of exception handling is that you no longer have to rack your brains to consider various errors. This provides a very effective method for handling certain types of errors, greatly improving programming efficiency. When abnormalities are triggered, it usually occurs:
The current code status is saved
code execution to be switched to the predefined abnormal processor function
According to the situation, the processor may start from the preserved code status again. Execute code, terminate script execution, or continue script execution from another location in the code
PHP 5 provides a new object-oriented error handling method. You can use try, throw, and catch exceptions. That is, use try to detect whether an exception is thrown. If an exception is thrown, use catch to catch the exception.
A try must have at least one corresponding catch. Define multiple catches to capture different objects. PHP will execute these catches in the order they are defined until the last one is completed. Within these catches, new exceptions can be thrown.
1. Use of exceptions
When an exception is thrown, the subsequent code will not continue to execute, and PHP will try to find a matching "catch" code block. If an exception is not caught and there is no need to use set_exception_handler() for corresponding processing, then PHP will generate a serious error and output a prompt message of Uncaught Exception...).
Throw an exception but don’t catch it:
<?php ini_set('display_errors', 'On'); error_reporting(E_ALL & ~ E_WARNING); $error = 'Always throw this error'; throw new Exception($error); // 继续执行 echo 'Hello World'; ?>
The above code will get a fatal error like this:
Fatal error: Uncaught exception 'Exception' with message 'Always throw this error' in E:\sngrep\index.php on line 5 Exception: Always throw this error in E:\sngrep\index.php on line 5 Call Stack: 0.0005 330680 1. {main}() E:\sngrep\index.php:0
2. Try, throw and catch
To avoid the above fatal error, you can use try catch to catch it.
Handling handlers should include:
Try - Functions that use exceptions should be located within the "try" code block. If no exception is triggered, the code continues execution as usual. But if an exception is triggered, an exception will be thrown.
Throw - This specifies how to trigger an exception. Each "throw" must correspond to at least one "catch"
Catch - The "catch" code block will catch the exception and create an object containing the exception information
Throw the exception and catch it, and you can continue execution The following code:
<?php try { $error = 'Always throw this error'; throw new Exception($error); // 从这里开始,tra 代码块内的代码将不会被执行 echo 'Never executed'; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(),'<br>'; } // 继续执行 echo 'Hello World'; ?>
In the "try" code block, check whether a "throw" exception is thrown, and an exception is thrown here.
The "catch" code block receives the exception and creates an object ($e) containing the exception information.
By calling $e->getMessage() from this exception object, the error message from the exception is output
In order to follow the principle of "each throw must correspond to a catch", you can set up a top-level exception handler to handle missed errors.
The above is the detailed content of Information sharing of the new PHP video tutorial of Band of Brothers. For more information, please follow other related articles on the PHP Chinese website!