Analyzing the differences between require and include from PHP core code _php Tutorial

Source: Internet
Author: User
Tags php source code
In-depth understanding of PHP require/include sequence http://www.jb51.net/article/25867.htm
popularity
In the PHP manual:

Require () is identical to include () except upon failure it would also produce a fatal e_error level ERROR. In other words, it'll halt the script whereas include () only emits a warning (e_warning) which allows the script to cont Inue.

This means that the require will abort PHP when it fails, and the include can continue to run.
What's the difference between inverted bottoms? We take this question together into PHP's core code.
Here is a diagram of the PHP running process (where does this figure come from?). The bird brother painted it? )

Tutorial: Lex is a code scanner, scanning code, YACC is yet another Compiler Compiler, the role is to convert any kind of code syntax into YACC syntax, YACC is the parser (real TMD).
Lex's suffix under C is *.l Yacc is *.y

Business
Here's how it looks:

cc@cc-laptop:/opt/workspace$ svn checkout Http://svn.php.net/repository/php/php-src/branches/PHP_5_3 php-src-5.3
Fetch the latest PHP source code from SVN.

Start in Depth:

cc@cc-laptop:/opt/workspace/php-src-5.3$ find. -type f-name "*.L"-exec grep-hn "require_once" {} \;
./zend/zend_language_scanner.l:1093: "Require_once" {
Look for the Lex code in the scanner file where the require_once appears, ZEND_LANGUAGE_SCANNER.L 1093 lines.
1093 "Require_once" {
1094 return t_require_once;
1095}

And then search the t_require_once,

cc@cc-laptop:/opt/workspace/php-src-5.3$ find. -type f-name "*.y"-exec grep-hn "T_include" {} \;
./zend/zend_language_parser.y:52:%left t_include t_include_once t_eval t_require T_REQUIRE_ONCE
./zend/zend_language_parser.y:985: | T_include Expr {zend_do_include_or_eval (zend_include, &$$, &$2 tsrmls_cc);}
./zend/zend_language_parser.y:986: | T_include_once Expr {zend_do_include_or_eval (zend_include_once, &$$, &$2 tsrmls_cc);}

In the vicinity of line 985, there is a bunch of code:

INTERNAL_FUNCTIONS_IN_YACC:
T_isset ' (' isset_variables ') ' {$$ = $;}
| T_empty ' (' variable ') ' {zend_do_isset_or_isempty (Zend_isempty, &$$, &$3 tsrmls_cc);}
| T_include Expr {zend_do_include_or_eval (zend_include, &$$, &$2 tsrmls_cc);}
| T_include_once Expr {zend_do_include_or_eval (zend_include_once, &$$, &$2 tsrmls_cc);}
| T_eval ' (' expr ') ' {zend_do_include_or_eval (Zend_eval, &$$, &$3 tsrmls_cc);}
| T_require Expr {zend_do_include_or_eval (Zend_require, &$$, &$2 tsrmls_cc);}
| T_require_once Expr {zend_do_include_or_eval (zend_require_once, &$$, &$2 tsrmls_cc);}
;

As a zend_do_include_or_eval, we need to go deeper into the search for

cc@cc-laptop:/opt/workspace/php-src-5.3$ find. -type f-name "*.c"-exec grep-hn "Zend_do_include_or_eval" {} \;
./zend/zend_compile.c:4317:void zend_do_include_or_eval (int type, Znode *result, const znode *OP1 tsrmls_dc)/* {{* * *

A structural body is assembled in the Zend_do_include_or_eval, Zend_include_or_eval.

and find Zend_vm_handler in the zend_vm_def.h (Zend_include_or_eval, const| tmp| var| CV, any):
Switch (Z_lval (opline->op2.u.constant)) {code slightly}

The key sentence in the middle is:
New_op_array = Compile_filename (Z_lval (opline->op2.u.constant), Inc_filename tsrmls_cc);

In the Zend_complie.h file:
Zend_api zend_op_array *compile_filename (int type, zval *filename tsrmls_dc);

This function is defined in the Zend_language_scaner.l file to find the most core code:

if (open_file_for_scanning (File_handle tsrmls_cc) ==failure) {
Differences between require and include: the display level of error messages (with bailout and no bailout)
if (type==zend_require) {//require
Zend_message_dispatcher (Zmsg_failed_require_fopen, File_handle->filename tsrmls_cc);
Zend_bailout ();
} else {
Zend_message_dispatcher (Zmsg_failed_include_fopen, File_handle->filename tsrmls_cc);
}
compilation_successful=0;
} else {code slightly}

Continue tracing Zend_message_dispatcher The Php_message_handler_for_zend function can be found in the Main/main.c file:

Include output error message at the level of: e_warning
Case Zmsg_failed_include_fopen:
Php_error_docref ("function.include" tsrmls_cc, E_warning, "Failed opening '%s ' for inclusion (include_path= '%s ')", php_ STRIP_URL_PASSWD ((char *) data), Str_print (PG (include_path)));
Break
Require the level of error message output is: E_compile_error
Code slightly

Summarize
In line with the beginning of the PHP manual, the difference between require and include is that when an error occurs, one of the errors is warning.

http://www.bkjia.com/PHPjc/322700.html www.bkjia.com true http://www.bkjia.com/PHPjc/322700.html techarticle In- depth understanding of PHP require/include sequence http://www.jb51.net/article/25867.htm popularized in PHP Manual: Require () is identical to include () Except upon failure it'll also produce ...

  • 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.