Only when a function is called can code statements in the function be executed to complete specific tasks. After the function is executed, the control will be returned to the place where the function is called, and the function will be able to return the information to the program in the form of a return value. Only when a function is called can code statements in the function be executed to complete specific tasks. After the function is executed, the control will be returned to the place where the function is called, and the function will be able to return the information to the program in the form of a return value. By using functions in the program, you can
Structured programming. In structured programming, each task is completed using an independent program code segment. While the function officially implements this"
Independent Cheng Xun code segment"The most ideal way, so the relationship between functions and structured programming is very close. The reason why Structured programming is excellent is that it has its own unique characteristics. the main reasons are as follows:
Two importantCause:
1. structured programs are easier to write because complex programming problems are divided into smaller and simpler tasks. Each task is completed by a function, and the code and variables in the function are independent of other parts of the program. By processing a simple task each time, the programming speed will be faster.
2. structured programs are easier to debug. If the program contains code that cannot be correctly run, the structured design reduces the problem to a specific single code segment, such as a specific function. In this way, debugging errors are easy to modify, from top to bottom, from left to right, step by step in order.
A significant advantage of structured programming is that it can save time. If you write a function to execute a specific task in a program, you can use it in another program that needs to execute the same task. Even if the heart program requires a slightly different job of Wang Cheng, it is easier to modify an existing function to write a new function. Think about it, you often use echo () and var_dump (), although you should first confirm the functions of the program, you must make some planning, all specific tasks to be executed by the program must be listed in the plan. Then, you can use the function to compile each specific task and call each task function in the execution order of the main program, forming a complete structured program. The following figure shows a program that contains three functions. each function executes a specific task and can be called once or multiple times in the main program. When a function is called, the control is passed to the function. After the function is executed, the control returns to the position where the function is called.
As shown in:
Php implements a typical dynamic language execution process: after a piece of code is obtained, the source code will be translated into commands (opcodes) after the lexical parsing and syntax parsing stages ), then the ZEND virtual machine sequentially executes these commands to complete the operation. Php itself is implemented using c, so all the functions that are finally called are c functions. In fact, we can regard php as a software developed by c. It is not difficult to see from the above description that the execution of functions in php is also translated into opcodes for calling. each function call actually executes one or more commands.
Zend describes each function based on the following data structure:
typedef union _zend_function { zend_uchar type; /* MUST be the first element of this struct! */ struct { zend_uchar type; /* never used */ char *function_name; zend_class_entry *scope; zend_uint fn_flags; union _zend_function *prototype; zend_uint num_args; zend_uint required_num_args; zend_arg_info *arg_info; zend_bool pass_rest_by_reference; unsigned char return_reference; } common;zend_op_array op_array; zend_internal_function internal_function; } zend_function;typedef struct _zend_function_state { HashTable *function_symbol_table; zend_function *function; void *reserved[ZEND_MAX_RESERVED_RESOURCES]; } zend_function_state;
The type indicates the function type: User function, built-in function, and overload function. Common contains basic information about a function, including the function name, parameter information, and function flag (Common function, static method, and abstract method. In addition, for user functions, there is also a function symbol table that records internal variables and so on. this will be detailed later. Zend maintains a global function_table, which is a large hahs table. When a function is called, the corresponding zend_function is first found from the table based on the function name. When a function is called, the virtual opportunity determines the call method based on the type. The Execution Principle of different types of functions is different.
[Recommended tutorials]
1. php1.cn Dugu jiujian (4)-php video tutorial
2. php programming from getting started to mastering a full set of video tutorials
3. php video tutorial
The above describes the working principle and structured programming of php user-defined functions. For more information, see other related articles in the first PHP community!