In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the benefits of official writing of php closures". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What are the advantages of official writing of php closures?
Specific problem description:
Copy $callback = function ($quantity, $product) use ($tax, & $total) {$pricePerItem = constant (_ _ CLASS__. ":: PRICE_" Strtoupper ($product); $total + = ($pricePerItem * $quantity) * ($tax + 1.0);}
What's the difference between this code and foreach? The number of cycles and the scale should be the same?
Answer:
Can make the code can be more reused, so that the functions provided have more possibilities.
For example, sorting is nothing more than comparing the size, and then changing places. The logic of switching locations is unified, but there are many cases compared to size, so a closure callback will be provided so that the place where it is used can decide who is big and who is small.
The other is to use some basic methods to make the code more readable.
For example, it should be more appropriate to change your column to array_reduce, so that others will probably realize what value you are trying to calculate to a single variable. However, it is best to write the closure function directly in the parameters.
For array traversal, in the early days, foreach will have a lot of problems, improper use will create unnecessary bug.
such as
/ / if used in this way, bugforeach ($arr as & $item) {$item ['ddd'] =' ddd';} / / may appear in subsequent logic so that array_walk ($arr, function (& $item) {$item ['ddd'] =' ddd';}) can be avoided)
Anyway, it has its advantages and disadvantages, and speed will certainly have an impact.
This is the end of the content of "what are the benefits of the official writing of php closures". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.