Thursday, August 28, 2025
HomeLanguagesPHP vfprintf() Function

PHP vfprintf() Function

The vfprintf() function is an inbuilt function in PHP that is used to write formatted information to a stream, such as a file or the output screen.

Syntax:

vfprintf(resource $stream, string $format, array $values): 

Parameters: This function accepts three parameters that are described below.

  • $stream: This parameter tells you where you write the string. It could be a file or a standard output like a screen.
  • $format: This is a template that shows how you want to arrange your data. It contains placeholders (like %d, %s, etc.) that represent where your actual data will go.
  • $values: This array holds the actual data that will be inserted into the placeholders of the format string.

Return Values: The vfprintf() function returns the count of the character written to the screen. It will return an integer type

Program 1: The following program demonstrates the vfprintf() function.

PHP




<?php
  
$stream = fopen('./output.txt', 'w');
$values = [10, "Ram"];
$format = "Number: %d, Name: %s\n";
     
$result = vfprintf($stream, $format, $values);
     
fclose($stream);
     
if ($result >= 0) {
    echo "Successfully wrote $result characters to the file.";
} else {
    echo "An error occurred while writing to the file.";
}   
  
?>


Output

Successfully wrote 22 characters to the file.

Program 2: The following program demonstrates the vfprintf() function.

PHP




<?php
  
$stream = fopen('php://stdout', 'w'); 
$values = ["Hello", "GPT-3.5", 2023];
$format = "Hello Geeks for Geeks .\n";
      
$result = vfprintf($stream, $format, $values);
      
if ($result >= 0) {
    echo "Successfully wrote $result characters to the screen.";
} else {
    echo "An error occurred while writing to the screen.";
}
      
fclose($stream);
  
?>


Output

Hello Geeks for Geeks .
Successfully wrote 24 characters to the screen.

Reference: https://www.php.net/manual/en/function.vfprintf.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32244 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6615 POSTS0 COMMENTS
Nicole Veronica
11787 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11832 POSTS0 COMMENTS
Shaida Kate Naidoo
6728 POSTS0 COMMENTS
Ted Musemwa
7009 POSTS0 COMMENTS
Thapelo Manthata
6684 POSTS0 COMMENTS
Umr Jansen
6697 POSTS0 COMMENTS