English | 简体中文 | 繁體中文
查询

readline_write_history()函数—用法及示例

「 将当前 readline 缓冲区的历史记录写入到历史记录文件中 」


函数名称:readline_write_history()

函数描述:该函数用于将当前 readline 缓冲区的历史记录写入到历史记录文件中。

适用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7

语法:bool readline_write_history ( string $filename )

参数:

  • filename:历史记录文件的路径和名称。如果文件不存在,则会创建一个新文件。

返回值:成功时返回 true,失败时返回 false。

示例:

// 创建一个 readline 缓冲区
readline_add_history('First command');
readline_add_history('Second command');

// 将 readline 缓冲区的历史记录写入文件
$filename = '/path/to/history.txt';
if (readline_write_history($filename)) {
    echo "History written to file successfully.";
} else {
    echo "Failed to write history to file.";
}

在上面的示例中,我们首先使用 readline_add_history() 函数将两个命令添加到 readline 缓冲区的历史记录中。然后,我们使用 readline_write_history() 函数将缓冲区的历史记录写入到指定的文件中。如果写入成功,将输出 "History written to file successfully.",否则将输出 "Failed to write history to file."。

请注意,readline_write_history() 函数必须在 readline 缓冲区被关闭之前调用,否则历史记录将不会被写入文件。

补充纠错
上一个函数: readlink()函数
下一个函数: readline_redisplay()函数
热门PHP函数
分享链接