fprintf

(PHP 5, PHP 7)

fprintf锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷址锟斤拷锟叫达拷氲斤拷锟�

说锟斤拷

fprintf ( resource $handle , string $format [, mixed $args [, mixed $... ]] ) : int

写锟斤拷一锟斤拷锟斤拷锟斤拷 format 锟斤拷式锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟� 锟斤拷 handle 锟斤拷锟斤拷蚩锟斤拷锟斤拷小锟�

锟斤拷锟斤拷

handle

锟侥硷拷系统指锟诫,锟角碉拷锟酵碉拷锟斤拷 fopen() 锟斤拷锟斤拷锟斤拷 resource(锟斤拷源)锟斤拷

format

锟轿硷拷 sprintf() 锟叫讹拷 format 锟斤拷锟斤拷锟斤拷锟斤拷

args

...

锟斤拷锟斤拷值

锟斤拷锟斤拷写锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷取锟�

锟斤拷锟斤拷

Example #1 fprintf(): zero-padded integers

<?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
?>

Example #2 fprintf(): formatting currency

<?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() - Return a formatted string
  • sscanf() - 锟斤拷锟斤拷指锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址锟�
  • fscanf() - 锟斤拷锟侥硷拷锟叫革拷式锟斤拷锟斤拷锟斤拷
  • vsprintf() - 锟斤拷锟截革拷式锟斤拷锟街凤拷锟斤拷
  • number_format() - 锟斤拷千位锟街革拷锟斤拷锟斤拷式锟斤拷式锟斤拷一锟斤拷锟斤拷锟斤拷