fwrite函数是PHP中用于向文件中写入数据的函数,它的语法如下: fwrite(file, string, length)
参数说明:
示例代码:
$file = fopen("test.txt", "w");
if ($file) {
$data = "Hello, World!";
fwrite($file, $data);
fclose($file);
echo "Data has been written to the file.";
} else {
echo "Unable to open the file.";
}
上述代码将字符串"Hello, World!"写入到名为test.txt的文件中。
关于fprintf函数,它与fwrite函数类似,不同之处在于它可以将数据写入到格式化字符串中。其语法如下: fprintf(file, format, arg1, arg2, ...)
参数说明:
示例代码:
$file = fopen("test.txt", "w");
if ($file) {
$name = "John";
$age = 25;
fprintf($file, "My name is %s and I am %d years old.", $name, $age);
fclose($file);
echo "Data has been written to the file.";
} else {
echo "Unable to open the file.";
}
上述代码将格式化字符串"My name is John and I am 25 years old."写入到名为test.txt的文件中。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站