Home  >  Article  >  Backend Development  >  How PHP custom functions work and introduction to structured programming

How PHP custom functions work and introduction to structured programming

伊谢尔伦
伊谢尔伦Original
2017-05-05 12:01:002195browse

When and only when the function is called, the code statements in the function will be executed, and its purpose is to complete some specific tasks. After the function is executed, control will return to the place where the function was called, and the function can return information to the program in the form of a return value. Structured programming is possible by using functions in your program. In structured programming, each task is completed piece by piece using independent program code. Functions are the most ideal way to formally implement this "Independent Cheng Xun code segment ", so the relationship between functions and structured programming is very close. Structured programming is excellent for its own unique reasons, mainly the following two important reasons:

1. Structured Programming Easier to write because complex programming problems are divided into multiple smaller, simpler tasks. Each task is performed by a function, and the code and variables in the function are independent of the rest of the program. By tackling one simple task at a time, programming becomes faster.

2. Structured programs are easier to debug. If there is some code in the program that does not run correctly, structured design allows the problem to be narrowed down to a specific single piece of code, such as a specific function. In this way, it is more convenient to debug errors and modify them. Just proceed step by step from top to bottom, from left to right.

One of the significant advantages of structured programming is that it can save time. If you write a function in one program that performs a specific task, you can use it in another program that needs to perform the same task. Even if the program requires a slightly different task, it's easier to modify an existing function than to rewrite it. Think about it, you often use echo() and var_dump(), although you should first confirm the function of the program, you must do some planning, and in the plan you must list all the specific tasks that the program will perform. Then use functions to write each specific task, and call each task function in the order of execution in the main program to form a complete structured program. Shown in the figure below is a program containing three functions. Each function performs a specific task and can be called once or multiple times in the main program. Whenever a function is called, control is passed to the function. After the function is executed, control returns to the location where the function was called

As shown in the figure below:

How PHP custom functions work and introduction to structured programming

php implements a typical dynamic language execution process : After getting a piece of code, after lexical analysis, syntax analysis and other stages, the source program will be translated into instructions (opcodes), and then the ZEND virtual machine will execute these instructions in sequence to complete the operation. Php itself is implemented in C, so the functions ultimately called are all C functions. In fact, we can regard PHP as a software developed in 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 instructions.

For each function, zend is described by 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;

type indicates the type of function: user function, built-in function, overloaded function . Common contains the basic information of the function, including function name, parameter information, function flags (ordinary functions, static methods, abstract methods), etc. In addition, for user functions, there is also a function symbol table that records internal variables, etc., which will be described in detail later. Zend maintains a global function_table, which is a large hash table. When a function is called, the corresponding zend_function will first be found from the table based on the function name. When making a function call, the virtual machine determines the calling method based on the type. Different types of functions have different execution principles.

【Recommended related tutorials】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. php programming from entry to master a full set of video tutorials

3. php practical video tutorial

The above is the detailed content of How PHP custom functions work and introduction to structured programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn