What is the difference between printf() and sprintf() in PHP?

printf() is used to output a formatted string to the screen, while sprintf() is used to store a formatted string in a variable. If you want to display the formatted string immediately, use printf(). If you need to store the formatted string for later use, use sprintf().

// Using printf()
$number = 10;
printf("The number is %d", $number);

// Using sprintf()
$number = 10;
$formatted_string = sprintf("The number is %d", $number);
echo $formatted_string;