PHP source code analysis

Source: Internet
Author: User
Tags sapi
In the Zend Directory, there are two files: zend_language_parser.y and zend_language_scanner.l. they are the Lex and Yacc script files, which generate the corresponding files. c and. in fact, this is very common in linux. gcc also uses them to generate a tree. For more information, see PHPCHINA. COM and author.


If you have any ideas after reading this article, please click here to post on the forum.


PHP source code analysis
1. directory structure
2. PHP uses Lex and Yacc to parse the syntax.
3. how does PHP use Mysql?
4. security mode?
5. which are PHP Standard functions and those are extension functions?
6. the PHP_FUNCTION (xx) macro in the PHP source code.
7. which function sets are standard?
8. implementation process of some functions
9. PHP function set registration process
10. interesting Zend LOGO images
11. PHP syntax tree?
12. obtain PHP source code from CVS


1. directory structure
1. build and compile related directories.
2. ext extension library code, such as Mysql, zlib, and iconv.
3. main directory.
4. sapi and various server interfaces, such as apache and IIS, also include common fastcgi and cgi.
5. compile PHP scripts in Windows and win32. WSH is used.
6. the core engine of the Zend folder.
7. script directory in scripts Linux.
8. tests test script Directory
9. sapi interfaces for various Web servers.


2. PHP uses Lex and Yacc to parse the syntax.
In the Zend Directory, there are two files: zend_language_parser.y and zend_language_scanner.l. they are the Lex and Yacc script files, which generate the corresponding files. c and. in fact, this is very common in linux, and gcc also uses them to generate a language tree.


3. how does PHP use Mysql?
The ext directory contains a mysql subdirectory in which php_mysql.c and php_mysql.h are responsible for PHP and Mysql operations. Uses the C language API in the Mysql manual.


4. security mode?
The safe_mode.h and safe_mode.c files in the main folder are responsible for the PHP Security mode.
5. which are PHP Standard functions and those are extension functions?
The ext directory is an extension, but there is still a standard folder under ext that stores standard functions in PHP. for example, The explode function is in. /ext/standard/string. c.
6. the PHP_FUNCTION (xx) macro in the PHP source code.
This macro is used to check whether a function name is valid. Valid function names should consist of lowercase letters and underscores.
7. which function sets are standard?
Through the./ext/standard/directory, we can see that the following common function sets are standard. String function set, array function set, file and directory operation function set, md5 algorithm, etc.
8. implementation process of some functions
1. implementation of fsockopen and pfsockopen
The implementation of these two functions is inseparable from the php_fsockopen_stream function in the./ext/standard/fsock. c file. The specific socket is implemented in./main/network. c.


Now.
9. PHP function set registration process
In./main/internal_functions.c, an array php_builtin_extensions contains the following members by default:


1. phpext_bcmath_ptr
2. phpext_calendar_ptr
3. phpext_com_dotnet_ptr
4. phpext_ctype_ptr
5. phpext_date_ptr
6. phpext_ftp_ptr
7. phpext_hash_ptr
8. phpext_odbc_ptr
9. phpext_pcre_ptr
10. phpext_reflection_ptr
11. phpext_session_ptr
12. phpext_spl_ptr
13. phpext_standard_ptr
14. phpext_tokenizer_ptr
15. phpext_zlib_ptr


Then register php_register_extensions (php_builtin_extensions, EXTCOUNT TSRMLS_CC ).


10. interesting Zend LOGO images
In the./main/logos. h file, the PHP and Zend labels are saved using the zend_logo and php_logo arrays. Therefore, you cannot find zend.gif in the issue package.
11. PHP syntax tree?


1. Lex and Yacc
This book is available on the market. You can take a look, including GCC, which is a syntax tree generated by their brothers. If you are interested in the compiler. You can read a few books on the market.


2. y syntax tree file
./Zend/zend_language_scanner.l and./Zend/zend_language_parser.y specify the PHP syntax. Literally, parser indicates a preliminary syntax scan, and parser indicates a syntax parsing. Based on the two files, lex and yacc can generate the corresponding c code. Therefore, syntax generation is convenient.


3. how to define a symbol
For example, if ($ language = 'php') is a token syntax, which is represented by T_IF. As defined in the. l file:


"If "{
Return T_IF;
}


In this way, if in the PHP file will be translated into the built-in symbol T_IF. '(Single quotes) is defined as follows:
['] {
BEGIN (ST_IN_SCRIPTING );
Return '\'';
}



4. composite symbols such as the most common variables $ discuz_user and $ submit.
"$" {LABEL }{
Zend_copy_value (zendlval, (yytext + 1), (yyleng-1 ));
Zendlval-> type = IS_STRING;
Return T_VARIABLE;
}



5. a valid if statement process
This definition is defined in zend_language_parser.y row 189:
T_IF '('expr ')'{
Zend_do_if_cond (& $3, & $4 TSRMLS_CC );
} Statement {
Zend_do_if_after_statement (& $4, 1 TSRMLS_CC );
} Elseif_list else_single {
Zend_do_if_end (TSRMLS_C );
}
| T_IF '('expr ')'':'{
Zend_do_if_cond (& $3, & $4 TSRMLS_CC );
} Inner_statement_list {
Zend_do_if_after_statement (& $4, 1 TSRMLS_CC );
} New_elseif_list new_else_single T_ENDIF ';'{
Zend_do_if_end (TSRMLS_C );
}
If must be followed by (). The expr expression in the circular arc is defined in row 734:
Expr:


R_variable {$ = $1 ;}
| Expr_without_variable {$ =$ 1 ;}
;


If can be followed by elseif statements and else statements.
From the syntax tree, we can see that if () is followed by:, which is rarely used.
6. priority and combination between left and right
Generally, the priority of the first defined operators in the. y file is relatively low, and % left and % right can be used to describe the combination between left and right. for example:


% Left '+ ''-''.'
% Left '*'/'%'
% Right '! '


This description '! 'In PHP syntax,' * '/' % '+'-'.' is a combination of left and '! 'Has a higher priority.
For example, syntax! $ A + $ B must be calculated first! $ A is performing the addition operation % left '.' is defined at the top, which indicates that it has the lowest priority, because we know that 'and' can be the same statement.
7. Analysis of php. ini


1. if the specified value is positive or negative?


[] * ("True" | "on" | "yes") [] * {
Ini_lval-> value. str. val = zend_strndup ("1", 1 );
Ini_lval-> value. str. len = 1;
Ini_lval-> type = IS_STRING;
Return success _true;
}


[] * ("False" | "off" | "no" | "none") [] * {
Ini_lval-> value. str. val = zend_strndup ("", 0 );
Ini_lval-> value. str. len = 0;
Ini_lval-> type = IS_STRING;
Return success _false;
}
12. obtain PHP source code from CVS
1. install the CVS tool, such as TortoiseCVS.
2. cvs-d: pserver: cvsread@cvs.php.net:/repository checkout php-src specific CVS use method please refer to CVS manual and other works.


The default configuration of the PHP. ini file is defined in./main. c 342.


--./Main. c -- 342:


/* {PHP_INI
*/
PHP_INI_BEGIN ()
PHP_INI_ENTRY_EX ("define_syslog_variables", "0", PHP_INI_ALL, NULL,


Php_ini_boolean_displayer_cb)
PHP_INI_ENTRY_EX ("highlight. bg", HL_BG_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)
PHP_INI_ENTRY_EX ("highlight. comment", HL_COMMENT_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)
PHP_INI_ENTRY_EX ("highlight. default", HL_DEFAULT_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)
PHP_INI_ENTRY_EX ("highlight.html", HL_HTML_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)
PHP_INI_ENTRY_EX ("highlight. keyword", HL_KEYWORD_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)
PHP_INI_ENTRY_EX ("highlight. string", HL_STRING_COLOR, PHP_INI_ALL, NULL,


Php_ini_color_displayer_cb)


More lines ......


PHP_INI_END ()
/*}}}*/


--./Main. c --
In the latest PHP version, memory_limit is changed from 8 MB to 16 MB.

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.