博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php生成pdf并生成_了解PHP生成器
阅读量:2512 次
发布时间:2019-05-11

本文共 5525 字,大约阅读时间需要 18 分钟。

php生成pdf并生成

When it comes to driving, speed is not everything. But on the web, speed makes all the difference. The faster your application, the better the user experience. Well, this article is on PHP Generators, so why are we talking about speed? As you are soon about to find out, generators make a huge difference on speed and memory management.

在驾驶方面,速度并不是决定一切。 但是在网络上,速度至关重要。 您的应用程序越快,用户体验就越好。 好吧,本文是关于PHP Generators的,那么为什么我们要谈论速度呢? 很快您就会发现,生成器在速度和内存管理上产生了巨大的差异。

( )

Added to PHP in version 5.5, generators are functions that provide a simple way to loop through data without the need to build an array in memory. Still a bit confused? An example is a good way to show generators in action.

在5.5版PHP中添加了生成器,这些生成器提供了一种简单的方法来遍历数据而无需在内存中构建数组的功能。 还是有点困惑? 一个例子是显示生成器运行情况的好方法。

First, let's quickly create a generator.php file that we will use throughout this tutorial. After creating the file, we add this little code snippet.

首先,让我们快速创建一个我们将在本教程中使用的generator.php文件。 创建文件后,我们添加此小代码段。

";}

We can quickly spin up an inbuilt PHP server in the directory where we created the generator.php file:

我们可以在创建generator.php文件的目录中快速启动内置PHP服务器:

php -S localhost:8000

So if we go to , we should get something like this.

因此,如果我们转到 ,我们应该得到类似的内容。

The code is pretty much self-explanatory, and this definitely doesn't look like much. But if we go back into our code and make a little change

该代码几乎是不言自明的,而且看起来绝对不是很多。 但是,如果我们返回代码并进行一些更改

";}

Now, the upper range(max) of generated numbers is PHP_INT_MAX, which is the largest number that your version of PHP can reach. After doing this, head over to the browser and refresh. But this time, you'll notice something different. The generator script throws a warning error.

现在,生成数字的上限(最大值)是PHP_INT_MAX ,这是您PHP版本可以达到的最大数字。 完成此操作后,转到浏览器并刷新。 但是这次,您会注意到一些不同的东西。 生成器脚本引发警告错误。

Well, that's a shame, PHP ran out of memory. Possible solutions that come to mind include going into php.ini and increasing memory_limit. Let's ask ourselves these questions, is this really effective? Do we want a single script to hog all our server's memory? The answers are no and no. This is not effective, and we do not want a single script to use up all our memory.

好吧,这很可惜,PHP用尽了内存。 我想到的可能解决方案包括进入php.ini和增加memory_limit 。 让我们问问自己这些问题,这真的有效吗? 我们是否要一个脚本来占用服务器的所有内存? 答案是否定的。 这是无效的,并且我们不希望单个脚本用完我们所有的内存。

( )

Let's define the same function above, call it with the same value PHP_INT_MAX and run it again. But, this time, we will be creating a generator function.

让我们定义上面的相同函数,使用相同的值PHP_INT_MAX调用它,然后再次运行它。 但是,这次,我们将创建一个生成器函数。

";}

Dissecting the getRange function, this time, we only loop through the values and yield an output. yield is similar to return as it returns a value from a function, but the only difference is that yield returns a value only when it is needed and does not try to keep the entire dataset in memory.

这次, getRange函数,我们仅循环遍历值并yield输出。 yield与return类似,因为yield从函数返回值,但是唯一的区别是yield仅在需要时才返回值,并且不尝试将整个数据集保留在内存中。

If you head over to your browser, you should see data being displayed on the page. Given the appropriate time, the browser eventually displays the data.

如果转到浏览器,应该会看到页面上显示的数据。 给定适当的时间,浏览器最终将显示数据。

Note: Generators can only be used from a function.

注意:只能通过函数使用生成器。

( )

There are times when we might want to parse a large dataset (it can be log files), perform computation on a large database result, etc. We don't want actions like this hogging all the memory. We should try to conserve memory as much as possible. The data doesn't necessarily need to be large — generators are effective no matter how small a dataset is. Don't forget, our aim is speed while using less memory.

有时,我们可能想解析大型数据集(可以是日志文件),对大型数据库结果执行计算等。我们不希望这样的操作占用所有内存。 我们应该尝试尽可能地节省内存。 数据不必一定很大-不管数据集多么小,生成器都是有效的。 别忘了,我们的目标是使用更少的内存来提高速度。

( )

There are times when our data only make sense when they are key-value based. When using generators, we can yield key-value pairs like this.

有时,只有当数据是基于键值时,我们的数据才有意义。 使用生成器时,我们可以产生这样的键值对。

$value; }}

We can then go ahead and use the pair as we would do with any array like this.

然后,我们可以像使用任何这样的数组一样继续使用该对。

$value) {
echo "Dataset { $range} has { $value} value
";}

( )

Generators can also take in values. This means that generators allow us to inject values into them, maybe as a command or something. For example, we can send a value to our generator telling to stop execution or change the output. Using the getRange function above, we can do this.

生成器也可以接受值。 这意味着生成器允许我们将值注入到它们中,可能是作为命令或其他方式。 例如,我们可以向生成器发送一个值,告诉它停止执行或更改输出。 使用上面的getRange函数,我们可以做到这一点。

To send inject this value, we can do this.

要发送注入该值,我们可以这样做。

send('stop'); } echo "Dataset { $range}
";}

NOTE: Using return in a generator breaks out of the generator function.

注意:在生成器中使用return会中断生成器功能。

( )

Using PHP_INT_MAX is a tad overboard. For me, PHP_INT_MAX is 2147483647 that is:

使用PHP_INT_MAX有点麻烦。 对我来说, PHP_INT_MAX2147483647 ,即:

two billion one hundred forty-seven million four hundred eighty-three thousand six hundred forty-seven

214.147.83.6.347

Generators are supposed to be memory efficient. This doesn't mean that they won't cause the same problem they are trying to solve if misused.

生成器应该是内存有效的。 这并不意味着如果使用不当,它们将不会引起他们试图解决的相同问题。

( )

Generators offer a significant performance boost that we cannot deny. Most times we don't need to have powerful servers to handle our code. We just need to do a little refactoring. Generators are useful and we ought to use them more often than not.

生成器极大地提高了我们无法否认的性能。 大多数时候,我们不需要强大的服务器来处理我们的代码。 我们只需要做一些重构。 生成器很有用,我们应该更多地使用它们。

翻译自:

php生成pdf并生成

转载地址:http://aguwd.baihongyu.com/

你可能感兴趣的文章
Annotation(注解)
查看>>
MySQL(四)--练习题
查看>>
高效掌握C#第五回---猜单词游戏
查看>>
07-Java 中的IO操作
查看>>
uclibc,eglibc,glibc之间的区别和联系【转】
查看>>
Java魔法堂:找外援的利器——Runtime.exec详解
查看>>
mysql数据库存放路径
查看>>
TestNG(五)常用元素的操作
查看>>
解决 Visual Studio 点击添加引用无反应的问题
查看>>
通过镜像下载Android系统源码
查看>>
python字符串格式化 %操作符 {}操作符---总结
查看>>
windows 不能在 本地计算机 启动 Apache
查看>>
iOS开发报duplicate symbols for architecture x86_64错误的问题
查看>>
Chap-6 6.4.2 堆和栈
查看>>
【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸
查看>>
C# MySql 连接
查看>>
网络抓包分析方法大全
查看>>
sql 数据类型
查看>>
android 截图
查看>>
WebServicer接口类生成方法。
查看>>