Introduction to PHP file loading and error handling

Source: Internet
Author: User
Tags define function php language
This article mainly introduces the PHP file loading and error handling of the introduction, has a certain reference value, now share to everyone, the need for friends can refer to

Knowledge Points:

1-File loading

2-Error handling

File loading

File load Statements

1) 4 File load statements:include, require, include_once, require_once

2) Use the same form. include the full path to the file to be loaded , or include (the full path of the file to be loaded);

Such as: include "d:/index.php";

3) files that can be loaded: PHP or HTML files

Path

1) relative path : Locates a loaded file location relative to the location of the current Web page file

./: Indicates the current location, that is, the location of the current Web page file (directory);
.. /: Indicates the previous position, that is, the location of the current Web page file in the upper level position (directory);

2) Absolute path

The local absolute path, for example: include "d:/demo/index.php";

Absolute path of the network; Such as: include "http://www.baidu.com/demo/index.php"

3) do not write path, direct file name

Load the specified file name under the current directory

Such as: include "index.php"; The PHP language engine will find the file in the current Web page directory

File loading in the execution process

Step 1: Exit PHP script mode from Include|require statement (enter HTML code mode) Step 2: Load the code in the file set by the Include|require statement and execute Step 3: Exit HTML mode to re-enter PHP script mode , continue after the code

Include,include_once, the difference between require,require_once

1) The difference between include and require, or the difference between include_once and require_once

Include or include_once loading the file fails (that is, the file was not found), reported " prompt error ", and then continue to execute subsequent code ;

Require or require_once loading the file fails, error and immediately terminate execution.

In general, require is used in programs where subsequent code depends on the loading of the file.

2) The difference between inlcude and include_once, or require and require_once

Files loaded with include or require do not determine whether they are duplicated, and as long as there is an include or require statement, they are loaded once---that can be loaded repeatedly.
Include_once or require_once loaded files will have internal judging mechanism whether the "previous code" has been loaded, if loaded, no longer loaded .
For example, if there are common ads on both sides of the page, use the Include

The use of return in a file that is loaded

1) Include,require,include_once,require_once Load statement, if load successfully returns 1, load failure returns false

2) If there is a return in the loaded file, the contents of the file after that return are not loaded--terminating loading

Can be used to: load a file to load a file to return a data

Error handling

Error classification

1) syntax error

If there are errors in the grammar, the error will be immediately and the program will not be executed.

2) Run-time error

After the program syntax check is passed, start running the program, and the errors encountered during this process

Common 3 Types of errors: Prompt error, warning error, fatal error

3) Logic Error

The program itself can be executed normally without error. But not the desired result.

Error rating

1) Technical level error rating: in PHP language, various errors are categorized in different levels

Each level of error, there is a "code", the code name is a "constant" inside the system

2) Common system errors

E_error: Fatal error
E_warning: Warning Error
E_notice: Suggestive Error

3) User-defined error

E_user_error: Custom Fatal error
E_user_warning: Custom Warning Error
E_user_notice: Custom Prompt error

4) Other

E_strict: Rigorous grammar check error
E_all: Represents all Errors

Detailed reference manual: function Reference "error handling and logging" for extension of PHP behavior predefined constants

1 <?php  2 function getbinstr ($e) {  3     $s = Decbin ($e);  This is a binary numeric string  4  /* 5         Str_pad ($str 1, length n, $str 2, Position W) function:  6 $str1 The             string, padding it with a string $str2 to the specified length n,  7             You can specify the position of the fill W, the left padding or the right padding  8     *     /9 $s 1 = str_pad ($s,, "0", str_pad_left);     return $s 1; "     <pre>",     echo "E_eeror =". E_error. "\t\t its corresponding binary value is:". Getbinstr (e_error);   1     echo "<br/>e_warning =". E_warning. "\t\t its corresponding binary value is:". Getbinstr (e_warning);  2     echo "<br/>e_notice =". E_notice. "\t\t its corresponding binary value is:". Getbinstr (e_notice);   8     echo "<br/>e_user_notice =". E_user_notice. "\t\t its corresponding binary value is:". Getbinstr (e_user_notice);   1024x768     echo "<br/>e_all =". E_all. "\t\t its corresponding binary value is:". Getbinstr (E_all);   32767     echo "</pre>";?>

View the binary number test for the error rating

Error triggering

1) Mode 1: System trigger

Typical error 3 types:

E_notice: Prompt Error: Error prompt is output and subsequent code is executed, e.g., using a nonexistent variable or constant

E_warning: Warning Error: Error prompt will be output and subsequent code to be executed; include loading a nonexistent file:

E_error: Fatal error: Causes the program to be unable to execute subsequent statements, such as: a nonexistent Function!!

2) Mode 2: Custom Trigger

1) Concept: When processing some data, the data itself is not wrong, but according to the needs of the specific application (business), the data will be required to meet a certain condition, and the data is not satisfied, you can "active" in the program to trigger (create) An error, to indicate the "illegality of the data."

2) Syntax form: trigger_error("error message Content", one of 3 user error codes);

If a user's fatal error (E_USER_ERROR) is triggered, subsequent execution of the program is terminated

Display of error reports

1) Error Report: Error message displayed on Web page

2) are error reports displayed? display_errors

Mode 1: Global Settings

Modify configuration file php.ini configuration item display_errors =on; Indicates that the display is off if it is off

Mode 2: Local Settings

Use the function Ini_set () in the php script file to set it up, such as Ini_set ("Display_errors", 0); Do not display error reports

This mode is set to take precedence over global settings

3) What level of error reporting is displayed? error_reporting

Prerequisite: Display_errorrs=on;

Mode 1: Global Settings

Modify configuration file php.ini configuration item error_reporting, such as: error_reporting = E_notice | e_warning | E_error

Mode 2: Local Settings

Use the function ini_set () in the php script file to set it up, such as Init_set ("error_reporting", E_notice | e_warning | E_error),

Logging of error logs

1) is the error log logged? log_errors

Mode 1: Global Settings

Modify the configuration file php.ini configuration item log_errors, such as: log_errors= on; Logging error logs

Mode 2: Local Settings

Use the function ini_set () in the php script file to set it up, such as Init_set ("Log_errors", 1);//Logging error log

      Get php.ini Configuration entry: ini_get ("Configuration item"); Gets the specified configuration item value for the php.ini

2) where is the record? Error_log

You can specify a location or record in the system log

In the specified location file: Directly using the file name, the system automatically establishes the filename under the folder and uses it to record the error message that occurs with all the Web page files under that folder .

Ini_set ("Error_log", "myError.txt"); If there is an error, it will be logged in the myError.txt file

Write to System log: ini_set ("Error_log", "syslog");//All error logs are logged to the system log file

Custom Error handlers

1) Error Handler: An error occurred, one way to handle the error. The essence is a function

2) Custom Error Handler: Change the original system processing error to developer custom error display and record processing

3) divided into 2 steps:

Step 1: Set the function Set_error_handler ("function name")for handling errors, such as Set_error_handler (' myerror ');

Step 2: Declare the function that defines the handling error . such as function Myerror ($errCode, $ERRMSG, $errFile, $errLine) {//error handling}

 1 <?php 2//Custom Error Processor 3//First step: Set the function name to be treated as Error 4 Set_error_handler ("My_error_handler"); 5 6//2nd step: Define function 7/** 8 * Custom error handling function 9 * This function is not called in the program, an error is automatically called, and the 4 real parameters are passed in the string $errCode error code  (level) * @param string $ERRMSG The contents of the error message * @param string $errFile The file name of the error that occurred * @param int $errLine represents the line number where the error occurred * @return void * * * function My_error_handler ($errCode, $ERRMSG, $errFile, $errLine) {$str = "; 18 $ str. = "<p><b><font color= ' red ' > Error:</font></b>"; $str. = "<br/> error code:". $errCode; $str. = "<br/> Error content is:". $errMsg; $str. = "<br/> Error file is:". $errFile; $str. = "<br/> Error line number is:". $errLine; $str. = "Time <br/> occurred:". Date ("y-m-d h:i:s"); $str. = "</p>";     Echo $str; Output the "Build" error complete processing results 26//can write the content to a file, both logging error log 27} 28 29//The following is the error code echo "<br/>aaaa";   echo $v 1;    Use a variable that does not exist. Echo C1; Using constants that do not exist 33 echo "<br/>bbbb"; echo "

Click to view custom error processor tests

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.