Difference between printf and sprintf PHP

Difference between printf and sprintf PHP

Difference between printf and sprintf PHP

The Sample  Code below shows the difference between printf vs sprintf

Difference

  • printf will directly print out the String
  • sprintf will save the value as a variable and print it out later

<?php



$name="Brian88";

// Print Out Using printf

//  1st %s is the first String refer to Brian88 2nd %s is the second string " Malaysia , %d refer to 37
	
printf("Hi my name is %s , I am from %s, I am %d years old","Brian88" ,"Malaysia" ,37 );

// Print Out Using sprintf

echo'<br>';

// Declare few Variable

$name="Brian88"; 
$country =" Malaysia";
$age=37;

//Declare the main Statement

$statement = " Hi my name is %s, I am from  %s , I am %d years old";

// Save the String Variable to an Output
$output = sprintf($statement,$name,$country,$age);

// echo output

echo $output;



?>





Leave a Reply

Your email address will not be published. Required fields are marked *

three × one =