How to write formatted string to a text file using fprintf() in PHP

2 Answers

0 votes
if (!($fp = fopen('d:\\data.txt', 'w'))) 
{
    echo "Error open file";
    return;
}

$day = 18;
$month = 6;
$year =  2016;

fprintf($fp, "%02d-%02d-%04d", $day, $month, $year);

/*
run: 

data.txt:
---------
18-06-2016

*/

 



answered Jun 18, 2016 by avibootz
0 votes
if (!($fp = fopen('d:\\data.txt', 'w'))) 
{
    echo "Error open file";
    return;
}

$money = 1235.89;

$character = fprintf($fp, '%01.2f', $money);

echo "wrote $character bytes to data.txt";

// data.txt:
// ---------
// 1235.89

/*
run: 

wrote 7 bytes to data.txt 

*/

 



answered Jun 18, 2016 by avibootz

Related questions

1 answer 261 views
1 answer 271 views
1 answer 212 views
1 answer 221 views
1 answer 219 views
219 views asked Nov 29, 2017 by avibootz
2 answers 242 views
242 views asked Dec 12, 2015 by avibootz
...