In this article, you will learn how to use functions wisely in php(as the current mainstream development language). In every high-level programming language, programmers can define functions, and php (as the current mainstream development language) is no exception. The only difference is that you don't have to worry about the function's return type.
Deep Study
Functions can be used to:
Encapsulate several lines of code into one statement.
Simplify the code.
Most importantly, treat the application as a coordinated product of smaller applications.
For developers who switch from compiled languages (such as C/C++) to php(as the current mainstream development language), php(as the current mainstream development language) The performance level of is astonishing. User-defined functions are very expensive in terms of using CPU and memory resources. This is mainly because php(as the current mainstream development language) is interpreted and loosely typed.
Wrapped or not
Some developers wrap every function they use simply because they don’t like the name, while other developers don’t like using it at all Package.
It is completely unacceptable to wrap existing php(as the current mainstream development language) functions without adding or supplementing existing functions. In addition to increasing size and execution time, such renamed functions can sometimes create an administrative nightmare.
Inline functions in the code will lead to inexplicable code and even greater management disasters. The only benefit of doing this may be faster code.
A more sensible approach is to only use the code multiple times and there are no built-in php (as the current mainstream development language) functions available for the task you want to achieve. Define functions. You can choose to rename it or use it with restrictions only when needed.
The chart in Figure 1 roughly shows how manageability and speed relate to the number of functions used. (I’m not specifying units here because numbers depend on individual and team capabilities; this relationship is important to visualize.)
Figure 1. Manageability/Speed vs. Number of functions
Named Functions
As I mentioned in Part 2 of this series (see Resources), to get efficient code For management, it is essential to always use a common naming convention.
Two other practices to consider are:
Choose names that give a good hint of the functionality of the function.
Use a prefix that indicates the package or module.
Suppose you have a module called user that contains user management functions. For functions that check whether the user is currently online, functions such as usr_is_online() and usrIsOnline Function names like () are good choices. Comparing the above names with function names like is_online_checker(), the conclusion is that using verbs is better than using nouns, because the function will always do it. What.
How many arguments?
Chances are you will be using an already constructed function, and even if that is not the case, you may want to maximize the scope of what you want to do. At this point, you and other developers should continue to develop easy-to-use functions. No one likes to use functions with obscure and difficult-to-understand parameters, so choose one that is easy to use. A name that explains the function's purpose (and reduces the number of parameters it takes) is a good way to ensure ease of use. What's the magic number for the number of parameters? In my opinion, more than three parameters make a function difficult to remember. Complex functions with a large number of parameters can almost always be split into simpler functions. No one likes using makeshift functions.
> Suppose you want to set the title of the HTML document before it is placed in the browser. The title is everything between the ... tags.
Suppose you want to set the title. and meta tags. Rather than using the setHeader(title, name, value) function to do all the work, a better solution is to use setTitle(title) and setMeta(name, value) to do each work independently. Setting the title and meta tags.
Thinking further, a title can contain only one title tag, but it can contain multiple meta tags. If multiple meta tags need to be set, the code must call setMeta() multiple times.In this case, a better solution is to pass a two-dimensional array of name-value pairs to setMeta() and have the function loop over the array — all at the same time.
In general, simultaneous functions like this are preferable. Calling a function once with all the data it needs to process is always better than calling the function multiple times and feeding it data incrementally. The main idea when writing functions is to minimize calls to them from other code.
http://www.bkjia.com/PHPjc/508604.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508604.htmlTechArticleIn this article, you will learn how to use functions wisely in PHP (as the current mainstream development language) . In every high-level programming language, programmers can define functions, php (as...