gpt4 book ai didi

php - fgets 和 current 有什么区别?

转载 作者:行者123 更新时间:2023-12-03 05:37:52 27 4
gpt4 key购买 nike

PHP manualSplFileObject 有两个看起来非常相似的方法:

$file->fgets()

Gets a line from the file.

$file->current()

Retrieves the current line of the file.

有关过程 fgets 的文档甚至更接近 current():

Gets line from file pointer.

但是没有任何说明其中一个是另一个的别名。两者都不带参数。这些有什么区别?

最佳答案

真正重要的区别是,例如我们有这个文件

foo
bar

下面的函数将打印 foo bar

$file = new \SplFileObject("test.txt");
while (!$file->eof()) {
echo $file->fgets();
}

但是这个函数会连续打印foo

$file = new \SplFileObject("test.txt");
while (!$file->eof()) {
echo $file->current();
}

因为fgets从begin开始并读取下一行(即第一行),然后它读取下一行(即秒行)并停止,因为它发现文件结尾但当前 总是读取当前行并且从不转到下一行,因此它永远不会中断循环,您需要使用 next 函数来读取下一行,因此第一个代码相当于:

$file = new \SplFileObject("test.txt");
while (!$file->eof()) {
echo $file->current();
$file->next();
}

编辑:另请检查 Josiah回答关于 flag 和 Axalix 的区别查看源代码差异

关于php - fgets 和 current 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30289793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com