sprintf

sprintf -- Return a formatted string

Description

string sprintf ( string format [, mixed args])





Przykład 1 .




This might output, "There are 5 monkeys in the tree". But imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:

Przykład 2 .




We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:

Przykład 3 .




An added benefit here is that you can repeat the placeholders without adding more arguments in the code. For example:

Przykład 4 .










Examples

Przykład 5 . sprintf( ) : zero-padded integers




Przykład 6 . sprintf( ) : formatting currency