Blanktext
Blanktext

Reputation: 1538

How do sign specifiers and padding specifiers work with printf() and sprintf() in PHP?

I am studying about printf,sprintf and i didnt understand few points, if can some one please help me understand thos points,

At This Link at PHP Manual:

There are explanations are numbered from one to six:

What i didnt understand is: The First and The Second(1(sign specifier), 2(padding specifier)), if can some one please help me with example for thos i will be very thankful.

Upvotes: 3

Views: 10979

Answers (4)

user8241064
user8241064

Reputation:

1.Sign specifier:

By default browsers only display - sign in front of negative numbers. + sign in front of positive numbers are omitted. But it is possible to instruct the browser to display + sign in front of positive numbers by using sign specifier. For example:

$num1=10;
$num2=-10;
$output=sprintf("%d",$num1);
echo "$output<br>";
$output=sprintf("%d",$num2);
echo "$output";

Output:

10
-10

Here the + sign before the positive number is omitted. However, if we put a + sign after % character of %d then the omission no longer takes place.

$num1=10;
$num2=-10;
$output=sprintf("%+d",$num1);
echo "$output<br>";
$output=sprintf("%+d",$num2);
echo "$output";

Output:

+10
-10

2.Padding specifier:

The padding specifier adds certain number of characters to the left or right of the output. The characters may be empty spaces, zeroes or any other ASCII character.

For example,

$str="hello";
$output=sprintf("[%10s]",$str);
echo $output;

Source code output:

[     hello]             //Total length of 10 positions,first five being empty spaces and remaining five being "hello"

HTML output:

 [ hello]                 //HTML displays only one empty space and collapses the rest, you have to use the <pre>...</pre> tag in the code for HTML to preserve the empty spaces.

Putting a negative sign left justifies the output:

$output=["%-10s",$string];
echo $output;

Source code output:

[hello     ]

HTML output:

[hello ]

Putting 0 after % sign replaces empty spaces with zeroes.

$str="hello";
$output=sprintf("[%010s]",$str);
echo $output;

Output:

[00000hello]

Left justification

$output=sprintf("[%-010s]",$str);

Output:

[hello00000]

Putting ' followed by any ASCII character like * after % results in the display of that ASCII character instead of empty spaces

$str="hello";
$output=sprintf("[%'*10s]",$str);
echo $output;

Output:

*****hello

Left justification:

$output=sprintf("[%-'*10s]",$str);
echo $output;

Output:

hello*****

Upvotes: 0

John Lawrence
John Lawrence

Reputation: 2923

The sign specifier: Placing a plus sign ( + ) forces negative AND positive signs to be visible (only negative values are specified by default).

$n = 1;

$format = 'With sign %+d  without %d';
printf($format, $n, $n);

Prints:

With sign +1 without 1

The padding specifier says what character will be used to pad the result to the specified length. The character is specified by prefixing it with a single quote ('). For example to pad to length 3 with the character 'a':

$n = 1;

$format = "Padded with 'a' %'a3d"; printf($format, $n, $n);
printf($format, $n, $n);

Prints:

Padded with 'a' aa1

Upvotes: 2

Alex Howansky
Alex Howansky

Reputation: 53543

The sign specifier forces a sign, even if it's positive. So, if you have

$x = 10;
$y = -10;
printf("%+d", $x);
printf("%+d", $y);

You'll get:

+10
-10

The padding specifier adds left padding so that the output always takes a set number of spaces, which allows you to align a stack of numbers, useful when generating reports with totals, etc.

 $x = 1;
 $y = 10;
 $z = 100;
 printf("%3d\n", $x);
 printf("%3d\n", $y);
 printf("%3d\n", $z);

You'll get:

   1
  10
 100

If you prefix the padding specifier with a zero, the strings will be zero padded instead of space padded:

 $x = 1;
 $y = 10;
 $z = 100;
 printf("%03d\n", $x);
 printf("%03d\n", $y);
 printf("%03d\n", $z);

Gives:

 001
 010
 100

Upvotes: 12

Hardik
Hardik

Reputation: 1411

sprintf() returns a string, printf() displays it.

The following two are equal:

printf(currentDateTime());
print sprintf(currentDateTime());

Upvotes: 30

Related Questions