PHP echo,print,printf,sprintf涵数中间的差别与使用方法详细说明
公布:smiling 来源于: PHP粉絲网 加上 访问: 评价:0
本文关键是对PHP中echo,print,printf,sprintf涵数中间的差别与使用方法开展了详尽的剖析详细介绍,必须的朋友能够回来参照下,期待对大伙儿有一定的协助。
1. echo涵数:
輸出涵数,是指令,不可以回到值。echo后边能够跟许多个主要参数,中间用分号分隔,如:
int printf ( string format [, mixed args [, mixed ...]] )
Produces output according to format , which is described in the documentation for sprintf() .
Returns the length of the outputted string.
把文本文件格式化之后輸出,如:
$age=25;
printf( my name is %s, age %d , $name, $age);
4. sprintf涵数:
string sprintf ( string format [, mixed args [, mixed ...]] )
Returns a string produced according to the formatting string format .
跟printf类似,但不复印,只是回到文件格式化后的文本,别的的与printf一样。
5. 详尽解读printf()涵数:
printf()涵数的启用文件格式为:
printf( 文件格式化标识符串 , 参数表
%d 十进制有标记整数金额
%u 十进制无标记整数金额
%f 浮等级
%s 标识符串
%c 单独标识符
%p 指针的值
%e 指数值方式的浮等级
%x, %X 无标记以十六进制表明的整数金额
%o 无标记以八进制表明的整数金额
%g 全自动挑选适合的表明法
表明:
(1). 能够在 % 和英文字母中间插到数据表明较大场宽。
①比如: %3d 表明輸出3位整型数, 不足3位右两端对齐。
②%9.2f 表明輸出场宽为9的浮等级, 在其中小多位为2, 整数金额位为6, 小数点占一名, 不足9位右两端对齐。
③%8s 表明輸出八个标识符的标识符串, 不足八个标识符右两端对齐。
④假如标识符串的长短、或整型多位数超出表明的场宽, 将按实际上际长短輸出。
⑤浮等级, 若整数金额一部分十位数超出了表明的整数金额位总宽, 将按具体整数金额位輸出;
⑥小数一部分十位数超出了表明的小多位总宽, 则按表明的总宽以四舍五入輸出。
⑦若想在輸出值前面一些0, 就应在场宽项前面个0。
比如: %04d 表明在輸出一个低于4位的标值时, 将在前边补0使其总总宽为4位。
⑧假如用浮等级表明标识符或整型量的輸出文件格式, 小数点后的数据意味着较大总宽, 小数点前的数据意味着最少总宽。
比如: %6.9s 表明显示信息一个长短很大于6且并不大于9的标识符串。若超过9, 则第9字符之后的內容将删掉除。
(2). 能够在 % 和英文字母中间加小书写母l, 表明輸出的是长型数。
①比如: %ld 表明輸出long整数金额
②%lf 表明輸出double浮等级
(3). 能够操纵輸出左两端对齐或右两端对齐, 即在 % 和英文字母中间添加一个 - 号可表明輸出为左两端对齐, 不然为右两端对齐。
①比如: %-7d 表明輸出7位整数金额左两端对齐
②%-10s 表明輸出10字符左两端对齐
(4). 一些独特要求标识符
①/n 换行
②/f 清屏并换页
③/r 回车键
④/t Tab符
⑤/xhh 表明一个ASCII码用16进表明,
⑥在其中hh是1到两个16进制数
6. printf() : examples
例1:various examples,编码以下:
$u = -;
$c = 65; // ASCII 65 is A
// notice the double %%, this prints a literal % character
printf( %%b = %b /n , $n); // binary representation
printf( %%c = %c /n , $c); // print the ascii character, same as chr() function
printf( %%d = %d /n , $n); // standard integer representation
printf( %%e = %e /n , $n); // scientific notation
printf( %%u = %u /n , $n); // unsigned integer representation of a positive integer
printf( %%u = %u /n , $u); // unsigned integer representation of a negative integer
printf( %%f = %f /n , $n); // floating point representation
printf( %%o = %o /n , $n); // octal representation
printf( %%s = %s /n , $n); // string representation
printf( %%x = %x /n , $n); // hexadecimal representation (lower-case)
printf( %%X = %X /n , $n); // hexadecimal representation (upper-case)
printf( %%+d = %+d /n , $n); // sign specifier on a positive integer
printf( %%+d = %+d /n , $u); // sign specifier on a negative integer
?
The printout of this program would be:
%b = 0101
%c = A
%d =
%e = 4.39518e+7
%u =
%u =
%f = .000000
%o =
%s =
%x = 29ea6ad
%X = 29EA6AD
%+d = +
%+d = -
printf( [%s]/n , $s); // standard string output
printf( [%10s]/n , $s); // right-justification with spaces
printf( [%-10s]/n , $s); // left-justification with spaces
printf( [%010s]/n , $s); // zero-padding works on strings too
printf( [% #10s]/n , $s); // use the custom padding character #
printf( [%10.10s]/n , $t); // left-justification but with a cutoff of 10 characters
?
The printout of this program would be:
[monkey]
[ monkey]
[monkey ]
[0000Monkey]
[####monkey]
[many monke]
?php
$isodate = sprintf( %04d-%02d-%02d , $year, $month, $day);
?
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output 123.1
$formatted = sprintf( %01.2f , $money);
// echo $formatted will output 123.10
?
$number = ;
echo sprintf( %.3e , $number); // outputs 3.63e+8
?
php实例教程网——出示php实例教程免费下载資源

Powered by php粉絲网 2010-2015 网站协作