Viewed   59 times

I am trying to learn more about the PHP function sprintf() but did not help me much as I am still confused, why would you want to use it?

Take a look at my example below.

Why use this:

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

When this does the same and is easier to write IMO:

$output = 'Here is the result: ' .$result. ' for this date ' .$date;

Am I missing something here?



sprintf has all the formatting capabilities of the original printf which means you can do much more than just inserting variable values in strings.

For instance, specify number format (hex, decimal, octal), number of decimals, padding and more. Google for printf and you'll find plenty of examples. The wikipedia article on printf should get you started.

Sunday, October 23, 2022

Isn't it just because you're echoing a printf?


$array = array("Mo" => "09:30-19:00",  
          "Di" => "09:30-19:00", 
          "So" => "geschlossen");

foreach( $array as $key => $value ){
     printf("%3s:%15s", $key, $value);

 Mo:    09:30-19:00 Di:    09:30-19:00 So:    geschlossen
Friday, November 4, 2022

You are correctly using the password_verify() function as long as $row['password'] is returning the correspondent password hash created by password_hash() of the plain password, and not the plain password itself or any other value than that.

Note that no further security concerns are included in this answer.

Wednesday, December 14, 2022

I got it.

I wrote:

echo printf(...);

instead of just:


After string produced by printf() i got that string length, which is value returned by printf (in fact - added number was 9 in case of 123.45 value, in 20.4 case it was number 8).

Wednesday, August 3, 2022

Use strftime instead of date:

setlocale( LC_TIME, "fr_FR" );
$formated_date_new = strftime( "%d %b %Y", strtotime( $this_date ) );

strftime refernce:

Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :