


PHP generator yield handles large amounts of data business (code example)
Official explanationyield
yield generator
is# Appeared after ##php5.5, the official document explains this:
yield provides an easier way to implement simple iteration objects, compared to defining a class to implement
Iterator Interface approach, performance overhead and complexity are greatly reduced.
yield keyword. A generator function looks like an ordinary function. The difference is: an ordinary function returns a value, while a generator can
yieldGenerates as many values as it needs. When the generator function is called, it returns an object that can be iterated over.
yield is somewhat similar to
return, but the difference is that
return will return a value and terminate the execution of the code, while
yield will return a value to the code that calls this generator in a loop and just pause execution of the generator function.
Here I would like to introduce to you the PHP version of non-buffered query
It means reading the data line by line into the PHP running memory. It is not a one-time read into the PHP running memory. As we all know, PHP has many built-in functions that can help us process the data. Because the data is in the memory, it can be operated, but the PHP running memory has a limit, and the default is 128M. Note: Because non-buffered queries will take a long time to connect to the database, it may cause slow queries, table locks, etc., which consume more mysql resourcesrelative to non-buffered queries The query is a buffered query:
If you use cached query, the PHP memory will directly explode and there will be insufficient memory. Okay, the main purpose here is to highlight yieldyield performance
The generator will have a very large impact on the performance of PHP applicationsSaving a lot of memory when PHP code is runningMore suitable for calculating large amounts of datayield operationUse
Generators allow you to write code in a foreach block to iterate over a set of data without creating an array in memory, which would hit your memory limit or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and instead of a normal function returning only once, the generator can yield as many times as needed to generate the values that need to be iterated over. Example explanation//仓库库存扣除测试 public function cangku_stock() { //set_time_limit(0); //表示永久运行,这里我是测试array的时候用到的 $order_info = $this->read_temp_api_order_info(10); //这里我就测试了10条数据,效果是看不出来的 foreach($order_info as $temp_api_order_info){ dd($temp_api_order_info); //打印出来看看数据 //处理数据 $api_ware_id = $this->o->getCangkuApiUrl() .'ware/program/addOutWare'; $out_wares = api_request($api_ware_id, $temp_api_order_info); $temp_out_wares = json_decode($out_wares, true); if ($temp_out_wares['code'] != 1) { $msg = (isset($temp_out_wares['msg']) && $temp_out_wares['msg']) ? $temp_out_wares['msg'] : var_export($out_wares, true); throw new Exception($msg); } } //dd("批量更新成功".date('Y-m-d H:i:s'));You can see that we called $order_info = $this->read_temp_api_order_info(10); and returned a Generator object. This object can be iterated using foreach. For each iteration, PHP will ask for a Generator instance. Calculate and provide the next value to iterate over. The elegance of the generator is reflected in the fact that each time a value is produced, the internal state of the generator will pause; when the next value is requested from the generator, the internal state will be restored. The generator's internal state switches between stalling and resuming until the end of the function definition is reached or an empty return statement is encountered. The effect is as follows:
$this->read_temp_api_order_info(10); If you are calculating the number of data tables, then you need to change this method. Try changing it yourself.
php tutorial!
The above is the detailed content of PHP generator yield handles large amounts of data business (code example). For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools