PHP echo,print,printf,sprintf函数之间的区别与用法详解


Posted in PHP onNovember 27, 2013

1. echo函数:

输出函数,是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:
echo $myvar1;
echo 1,2,$myvar,"<b>bold</b>";

2. print函数:

是函数,可以返回一个值,只能有一个参数。

int print ( string arg )

Outputs arg . Returns 1 , always.

3. printf函数:

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.

把文字格式化以后输出,如:
$name="hunte";
$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 表示输出8个字符的字符串, 不够8个字符右对齐。

 ④如果字符串的长度、或整型数位数超过说明的场宽, 将按其实际长度输出。

 ⑤浮点数, 若整数部分位数超过了说明的整数位宽度, 将按实际整数位输出;

 ⑥小数部分位数超过了说明的小数位宽度, 则按说明的宽度以四舍五入输出。

 ⑦若想在输出值前加一些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到2个16进制数

6. printf() : examples

例1: various examples

<?php  
$n =  43951789;  
$u = -43951789;  
$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 = '10100111101010011010101101'  
%c = 'A'  
%d = '43951789'  
%e = '4.39518e+7'  
%u = '43951789'  
%u = '4251015507'  
%f = '43951789.000000'  
%o = '247523255'  
%s = '43951789'  
%x = '29ea6ad'  
%X = '29EA6AD'  
%+d = '+43951789'  
%+d = '-43951789'

例2: string specifiers
<?php  
$s = 'monkey';  
$t = 'many monkeys';  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]

例3:zero-padded integers
<?php  
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);  
?> 

例4:formatting currency
<?php  
$money1 = 68.75;  
$money2 = 54.35;  
$money = $money1 + $money2;  
// echo $money will output "123.1";  
$formatted = sprintf("%01.2f", $money);  
// echo $formatted will output "123.10"  
?>

例5: sprintf() : scientific notation
<?php  
$number = 362525200;  echo sprintf("%.3e", $number); // outputs 3.63e+8  
?> 
PHP 相关文章推荐
Windows下PHP的任意文件执行漏洞
Oct 09 PHP
解决MySQL中文输出变成问号的问题
Jun 05 PHP
PHP ajax 分页类代码
Nov 13 PHP
cmd下运行php脚本
Nov 25 PHP
PHP 程序员应该使用的10个组件
Oct 31 PHP
PHP HTML JavaScript MySQL代码如何互相传值的方法分享
Sep 30 PHP
php中filter函数验证、过滤用户输入的数据
Jan 13 PHP
php实现Session存储到Redis
Nov 11 PHP
Thinkphp和onethink实现微信支付插件
Apr 13 PHP
PHP中让json_encode不自动转义斜杠“/”的方法
Feb 28 PHP
PHP使用pdo实现事务处理操作示例
Sep 05 PHP
Laravel5.5+ 使用API Resources快速输出自定义JSON方法详解
Apr 06 PHP
php strnatcmp()函数的用法总结
Nov 27 #PHP
PHP 正则判断中文UTF-8或GBK的思路及具体实现
Nov 26 #PHP
php对二维数组按指定键值key排序示例代码
Nov 26 #PHP
PHP上传文件时文件过大$_FILES为空的解决方法
Nov 26 #PHP
PHP获取当前url的具体方法全面解析
Nov 26 #PHP
php 批量替换html标签的实例代码
Nov 26 #PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
Nov 26 #PHP
You might like
PHP获取文件夹大小函数用法实例
2015/07/01 PHP
PHP中的静态变量及static静态变量使用详解
2015/11/05 PHP
详解WordPress开发中wp_title()函数的用法
2016/01/07 PHP
laravel实现于语言包的完美切换方法
2019/09/29 PHP
拉动滚动条加载数据的jquery代码
2012/05/03 Javascript
JavaScript 命名空间 使用介绍
2013/08/29 Javascript
JavaScript判断变量是否为空的自定义函数分享
2015/01/31 Javascript
jQuery实现带滚动导航效果的全屏滚动相册实例
2015/06/19 Javascript
JavaScript调用浏览器打印功能实例分析
2015/07/17 Javascript
jQuery旋转木马式幻灯片轮播特效
2015/12/04 Javascript
JS字符串统计操作示例【遍历,截取,输出,计算】
2017/03/27 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
使用JavaScript实现在页面中显示距离2017年中秋节的天数
2017/09/26 Javascript
如何在JavaScript中优雅的提取循环内数据详解
2019/03/04 Javascript
layui监听工具栏的实例(操作列表按钮)
2019/09/10 Javascript
[01:29]Ti4循环赛第三日精彩回顾
2014/07/13 DOTA
[22:20]初生之犊-TI4第5名LGD战队纪录片
2014/08/13 DOTA
PyQt5利用QPainter绘制各种图形的实例
2017/10/19 Python
Python环境搭建之OpenCV的步骤方法
2017/10/20 Python
Python使用装饰器进行django开发实例代码
2018/02/06 Python
Python爬取成语接龙类网站
2018/10/19 Python
教你一步步利用python实现贪吃蛇游戏
2019/06/27 Python
python global关键字的用法详解
2019/09/05 Python
Flask框架搭建虚拟环境的步骤分析
2019/12/21 Python
解决tensorflow由于未初始化变量而导致的错误问题
2020/01/06 Python
python爬虫使用requests发送post请求示例详解
2020/08/05 Python
泰国国际航空公司官网:Thai Airways International
2019/12/04 全球购物
日本最大的彩色隐形眼镜销售网站:CharmColor
2020/09/09 全球购物
电子商务应届生自我鉴定
2014/01/13 职场文书
研究生导师评语
2014/12/31 职场文书
高一军训决心书
2015/02/05 职场文书
自主招生英文自荐信
2015/03/25 职场文书
2015年采购部工作总结
2015/04/23 职场文书
社区党务工作总结2015
2015/05/19 职场文书
MySQL如何构建数据表索引
2021/05/13 MySQL
python ConfigParser库的使用及遇到的坑
2022/02/12 Python