-1

Possible Duplicates:
PHP sprintf() and printf() functions
Why use sprintf function in PHP?

Printf and sprintf where exactly used in php? give one example?

Community
  • 1
  • 1
Karthik
  • 1,428
  • 3
  • 17
  • 29

2 Answers2

3

printf function in php is used to output a formatted string

For example:

<?php
$format = 'I have %d questions to %s';
printf($format, 2, 'answer');
?>

This will output as follows: I have 2 questions to answer


sprintf function in php is used to return a formatted string.

For Example:

<?php
$format = 'I have %d questions to %s';
$a_string = sprintf($format, 2, 'answer');
echo $a_string; 
?>

This will output as follows: I have 2 questions to answer


In the second example sprintf function returns a formatted string and gets stored into the variable $a_string.

GDP
  • 8,109
  • 6
  • 45
  • 82
Hassan
  • 31
  • 2
0

Using sprintf have many options to format your output string. This SO answer will help you more:

Why use sprintf function in PHP?

Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131