MENU
Display and Formatting
To produce or output a string, use:print ($s) or echo ($s)
printf($format[,$m1……])
sprintf($format[,$m1……])
fprintf($resource,$format[,$m1……])
vprintf($format,$arr)
vsprintf($format,$arr)
vfprintf($resource,$format,$arr)
sscanf($s1,$format[,&$m1……])
printf(), fprint(), vprintf(), vfprintf() returns the length of the string written. The prefix ‘s’ returns a string. The prefix ’f’ prints the string on the resource provided. The prefix ‘v’ uses an array, instead of a variable-length arguments list.
sscanf() parses $s1 according to $format and stores the values in $m1……If we pass only 2 arguments, it will return an array. Otherwise the number of values assigned will be returned.
$format can contain the following conversion specifiers, which are preceded by the % sign. | ||
spc | Treated as | Presented as |
b | integer | binary number |
c | integer | character of that ASCII value |
d | integer | decimal number |
e | scientific notation | scientific notaion |
u | integer | unsigned decimal number |
f | float | float (locale) |
F | float | float (non-locale) |
o | integer | octal number |
s | string | string |
x | integer | lowercase hex |
X | integer | uppercase hex |
E | like %e but uses uppercase E | |
g | shorter of %e and %f | |
G | shorter of %E and %f | |
% | The % literal | |
Between the % sign and the specifier symbol, additional specifications may be defined to format the output. See the example below. |
number_format($f,$i=0) and number_format ($f, $i=0, $s1=’.’, $s2=’,’) return a string that is $f formatted with grouped thousands. $i sets the number of decimal points. $s1 is the separator for the decimal point. $s2 is the thousands separator.
<!DOCTYPE html><html><head></head>
<body><?php
printf("%d ducks and %d %s<br />",20,3,"alligators");
vprintf("%d ducks and %d %s<br />",[20,3,"alligators"]);
printf("the number is %03d<br />",7);
printf("the value of PI is %.3f<br />",3.14159265359);
printf("the hex of 110 is %X<br />",110);
printf("%+.3e%%<br />",6425.6892);
printf("%c<br />",65);
sscanf("20 ducks and 3 alligators","%d ducks and %d %s",$a,$b,$c);
echo "$a pigs and $b $c<br />";
list($m,$d,$y)=sscanf("February 02 2002", "%s %d %d");
echo "$d $m $y<br />";
echo number_format(1234567890.123456,2,'|',' ');
?></body></html>
20 ducks and 3 alligators 20 ducks and 3 alligators the number is 007 the value of PI is 3.142 the hex of 110 is 6E +6.426e+3% A 20 pigs and 3 alligators 2 February 2002 1 234 567 890|12