Introduction: Why fprintf() Is Important in PHP

When working with PHP, you often need to format output before displaying it or saving it to a file.
Simple echo statements work, but they are limited when you need structured, formatted text.

This is where the PHP fprintf() function becomes useful.

It allows you to:

  • Format text with numbers, strings, decimals, etc.

  • Write formatted output directly to a file or stream

  • Create professional logs, reports, and formatted data

This guide explains fprintf() in a clear, teacher-style way, perfect for PHP beginners.

What is fprintf() in PHP?

The fprintf() function formats a string and writes it to a stream, such as:

  • A file

  • Standard output (STDOUT)

  • Other writable streams

👉 In simple words:
fprintf() formats text and sends it to a file or output destination.

Syntax of fprintf()

int fprintf(resource $stream, string $format, mixed ...$values)

Parameters Explained Simply

ParameterDescription
streamWhere the output is written (file or output stream)
formatA string with formatting placeholders
valuesVariables to insert into the format

Example 1: Basic fprintf() Usage

<?php
fprintf(STDOUT, "Hello, %s!", "World");
?>

Output

 
Hello, World!

Explanation

  • %s → placeholder for a string

  • "World" replaces %s

Common Format Specifiers (Very Important)

SpecifierMeaning
%sString
%dInteger
%fFloating-point number
%.2fFloat with 2 decimal places
%cCharacter

Example 2: Formatting Numbers

<?php
fprintf(STDOUT, "Age: %d, Score: %.2f", 25, 89.567);
?>

Output

Age: 25, Score: 89.57

Using fprintf() with Files (Most Common Use Case)

One of the most powerful uses of fprintf() is writing formatted data into files.

Example 3: Writing to a File

<?php
$file = fopen("data.txt", "w");

fprintf($file, "Name: %s\n", "Amit");
fprintf($file, "Marks: %d\n", 92);

fclose($file);
?>

File Output (data.txt)

 
Name: Amit Marks: 92

Example 4: Writing Multiple Values

<?php
$file = fopen("students.txt", "w");

fprintf($file, "Student: %s | Age: %d | Grade: %s\n", "Neha", 20, "A");

fclose($file);
?>

Difference Between printf() and fprintf()

Featureprintf()fprintf()
Output locationBrowser / screenFile or stream
Formatting✅ Yes✅ Yes
Requires stream❌ No✅ Yes
Common useDisplay outputWrite to files

👉 Key Difference:
printf() displays formatted text, while fprintf() writes it somewhere.

Return Value of fprintf()

  • Returns the number of characters written

  • Returns false on failure

<?php
$count = fprintf(STDOUT, "Hello %s", "PHP");
echo "\nCharacters written: $count";
?>

Real-World Use Case: Log File Creation

<?php
$log = fopen("log.txt", "a");

fprintf($log, "[%s] User logged in: %s\n", date("Y-m-d H:i:s"), "admin");

fclose($log);
?>

👉 Used in:

  • System logs

  • Error logs

  • Activity tracking

  • Reports

Common Beginner Mistakes

  •  Forgetting to open the file before using fprintf()
  •  Using wrong format specifiers
  •  Not closing the file
  • Passing mismatched values
  • Confusing printf() with fprintf()

Best Practices for Using fprintf()

  •  Always open files with correct mode (w, a, etc.)
  • Match format specifiers with variable types
  •  Close files after writing
  • Use \n for new lines
  •  Validate file operations

Security & Safety Tips

  • Never write unsanitized user input directly to files

  • Validate data before formatting

  • Handle file permissions carefully

FAQs: PHP fprintf() Function

What does fprintf() do in PHP?

It formats a string and writes it to a file or output stream.

Can fprintf() write to the browser?

Yes, using STDOUT.

What is the difference between printf and fprintf?

printf() outputs to screen, fprintf() writes to a stream.

Is fprintf() useful for logging?

Yes, it is commonly used for log files.

Does fprintf() return anything?

Yes, the number of characters written.