首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

fprintf

(PHP 5, PHP 7)

fprintf - 将格式化的字符串写入流

描述

代码语言:javascript
复制
int fprintf ( resource $handle , string $format [, mixed $args [, mixed $... ]] )

将根据格式生成的字符串写入由句柄指定的流资源。

参数

handle

通常使用fopen()创建的文件系统指针资源。

format

有关格式的描述,请参阅sprintf()。

args ...

返回值

返回写入的字符串的长度。

例子

示例#1 fprintf():零填充整数

代码语言:javascript
复制
<?php
if?(!($fp?=?fopen('date.txt',?'w')))?{
????return;
}

fprintf($fp,?"%04d-%02d-%02d",?$year,?$month,?$day);
//?will?write?the?formatted?ISO?date?to?date.txt
?>

示例#2 fprintf():格式化货币

代码语言:javascript
复制
<?php
if?(!($fp?=?fopen('currency.txt',?'w')))?{
????return;
}

$money1?=?68.75;
$money2?=?54.35;
$money?=?$money1?+?$money2;
//?echo?$money?will?output?"123.1";
$len?=?fprintf($fp,?'%01.2f',?$money);
//?will?write?"123.10"?to?currency.txt

echo?"wrote?$len?bytes?to?currency.txt";
//?use?the?return?value?of?fprintf?to?determine?how?many?bytes?we?wrote
?>

扩展内容

  • printf() - 输出格式化的字符串
  • sprintf() - 返回格式化的字符串
  • sscanf() - 根据格式解析字符串的输入
  • fscanf() - 根据格式解析文件的输入
  • vsprintf() - 返回格式化的字符串
  • number_format() - 用分组数千格式化数字

← explode

get_html_translation_table →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com