PHP print类函数使用总结


Posted in PHP onJune 25, 2010
<?php 
/*************by garcon1986*********/ 
//print和echo的区别: 
//1. echo可以输入多个字符串,而print不能。 
print "hello"."world"; //成功 
echo "hello"."world"; //成功 
//print "hello","world"; //失败 
echo "hello","world"; //成功 
//2. echo比print更快。 
$stime = microtime(true); 
print "hello"."world"; 
$etime = microtime(true); 
$total = $etime - $stime; 
echo $total.'<br/>'; 
//microtime — Return current Unix timestamp with microseconds 
$stime2 = microtime(true); 
echo "hello"."world"; 
$etime2 = microtime(true); 
$total2 = $etime2 - $stime2; 
echo $total2.'<br/>'; 
//执行结果: 
//helloworld0.0014331340789795 
//helloworld0.00018310546875 
//看到echo比print更快。 
//print_r — Prints human-readable information about a variable 或数组 
$a = "sajfd sfjal sfjalwureoi weu sj we fk io "; 
print_r($a); 
echo '<br />'; 
$a = array("b","c","d"); 
print_r($a); 
echo '<br />'; 
//var_dump — Dumps information about a variable 或数组 
//var_dump -- 打印变量的相关信息 
$a = "sajfd sfjal sfjalwureoi weu sj we fk io "; 
var_dump($a); 
echo '<br />'; 
$a = array("b","c","d"); 
var_dump($a); 
echo '<br />'; 
var_dump(array("b","c","d")); 
echo '<br />'; 
?> 
<?php 
/************by garcon1986********/ 
//%% - 返回百分比符号 
//%b - 二进制数 
//%c - 依照 ASCII 值的字符 
//%d - 带符号十进制数 
//%e - 可续计数法(比如 1.5e+3) 
//%f - 浮点数(local settings aware) 
//%F - 浮点数(not local settings aware) 
//%o - 八进制数 
//%s - 字符串 
//%u - 无符号十进制数 
//%x - 十六进制数(小写字母) 
//%X - 十六进制数(大写字母) 
//printf()函数输出格式化的字符串 
$str = "hello"; 
$number = 456; 
//example1 
printf("%s world. Day number %s", $str, $number); //输出: hello world. Day number 456 
print "<br/>"; 
//example2 
printf("%%", $number); //% 
print "<br/>"; 
printf("%b", $number); //111001000 
print "<br/>"; 
printf("%c", $number); //ascii码 
print "<br/>"; 
printf("%d", $number); //456 
print "<br/>"; 
printf("%e", $number); //4.560000e+2 
print "<br/>"; 
printf("%f", $number); //456.000000 
print "<br/>"; 
printf("%F", $number); //456.000000 
print "<br/>"; 
printf("%o", $number); //710 
print "<br/>"; 
printf("%s", $number); //456 
print "<br/>"; 
printf("%u", $number); //456 
print "<br/>"; 
printf("%x", $number); //1c8 
print "<br/>"; 
printf("%X", $number); //1C8 
print "<br/>"; 
printf("With 2 decimals: %1\$.2f<br />With no decimals: %1\$u<br />",$number); 
//With 2 decimals: 456.00 
//With no decimals: 456 
printf("With 2 decimals: %f<br />With no decimals: %1\$u<br />",$number); 
//With 2 decimals: 456.000000 
//With no decimals: 456 
//fprintf() 函数把格式化的字符串写到指定的输出流(例如:文件或数据库)。 
$file = fopen("text.txt","w"); 
echo fprintf($file, "fprintf 1: %s world. Day number %u", $str, $number).'<br/>'; //38 
echo fprintf($file, "fprintf 2: %f", $number).'<br/>'; //21 
echo fprintf($file,"fprintf 3: With 2 decimals: %1\$.2f\nWith no decimals: %1\$u",$number).'<br />'; //56 
//vprintf()函数输出格式化的字符串。 
//vprintf() 中的 arg 参数位于数组中。数组的元素会被插入主字符串的百分比 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。 
vprintf("vprintf: %s world. Day number %u", array($str,$number)); //vprintf: hello world. Day number 456 
echo '<br />'; 
//sprintf() 函数把格式化的字符串写写入一个变量中。 
$txt = sprintf("sprintf: %s world. Day number %u",$str,$number); 
echo $txt.'<br />'; //sprintf: hello world. Day number 456 
//vfprintf() Operates as fprintf() but accepts an array of arguments, rather than a variable number of arguments. 
echo vfprintf($file, "vfprintf: %s world! Day number %u", array($str, $number)).'<br />'; //37 
//vsprintf() Operates as sprintf() but accepts an array of arguments, rather than a variable number of arguments. 
$txt = vsprintf("vsprintf: %s world. Day number %u",array($str,$number)); 
echo $txt.'<br />'; //vsprintf: hello world. Day number 456 
?>
PHP 相关文章推荐
第十二节 类的自动加载 [12]
Oct 09 PHP
常用的php ADODB使用方法集锦
Mar 25 PHP
PHP Pear 安装及使用
Mar 19 PHP
php ss7.5的数据调用 (笔记)
Mar 08 PHP
PHP中最容易忘记的一些知识点总结
Apr 28 PHP
对淘宝URL中ID提取的PHP代码
Sep 01 PHP
php获取字段名示例分享
Mar 03 PHP
php发送post请求函数分享
Mar 06 PHP
php定义参数数量可变的函数用法实例
Mar 16 PHP
Apache无法自动跳转却显示目录的解决方法
Nov 30 PHP
laravel 数据验证规则详解
Oct 23 PHP
PHP实现二维数组(或多维数组)转换成一维数组的常见方法总结
Dec 04 PHP
php url地址栏传中文乱码解决方法集合
Jun 25 #PHP
PHP+ACCESS 文章管理程序代码
Jun 21 #PHP
php $_SERVER[&quot;REQUEST_URI&quot;]获取值的通用解决方法
Jun 21 #PHP
php自动加载的两种实现方法
Jun 21 #PHP
PHP定时自动生成静态HTML的实现代码
Jun 20 #PHP
PHP XML操作的各种方法解析(比较详细)
Jun 17 #PHP
PHP操作xml代码
Jun 17 #PHP
You might like
php递归实现无限分类生成下拉列表的函数
2010/08/08 PHP
PHP会话处理的10个函数
2015/08/11 PHP
推荐10个超棒的jQuery工具提示插件
2011/10/11 Javascript
JavaScript高级程序设计(第3版)学习笔记9 js函数(下)
2012/10/11 Javascript
Extjs407 getValue()和getRawValue()区别介绍
2013/05/21 Javascript
JavaScript中的连字符详解
2013/11/28 Javascript
javascript实现复制与粘贴操作实例
2014/10/16 Javascript
JQuery导航菜单选择特效
2016/04/11 Javascript
AngularJS入门教程之表格实例详解
2016/07/27 Javascript
Javascript 6里的4个新语法
2016/08/25 Javascript
canvas实现粒子时钟效果
2017/02/06 Javascript
vue 过滤器filter实例详解
2018/03/14 Javascript
vue项目中api接口管理总结
2018/04/20 Javascript
如何用Node写页面爬虫的工具集
2018/10/26 Javascript
更优雅的微信小程序骨架屏实现详解
2019/08/07 Javascript
使用 webpack 插件自动生成 vue 路由文件的方法
2019/08/20 Javascript
Flexible.js可伸缩布局实现方法详解
2020/11/13 Javascript
[02:44]DOTA2英雄基础教程 钢背兽
2013/12/19 DOTA
python清除字符串里非字母字符的方法
2015/07/02 Python
python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法
2017/05/24 Python
Scrapy的简单使用教程
2017/10/24 Python
linux环境下的python安装过程图解(含setuptools)
2017/11/22 Python
对pandas的层次索引与取值的新方法详解
2018/11/06 Python
python中PS 图像调整算法原理之亮度调整
2019/06/28 Python
python 操作hive pyhs2方式
2019/12/21 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
2020/01/10 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
2020/09/01 Python
python线程池 ThreadPoolExecutor 的用法示例
2020/10/10 Python
职业规划书如何设计?
2014/01/09 职场文书
会计专业大学生求职信范文
2014/01/28 职场文书
经典促销广告词大全
2014/03/19 职场文书
经理助理岗位职责
2015/02/02 职场文书
《分一些蚊子进来》读后感3篇
2020/01/09 职场文书
python munch库的使用解析
2021/05/25 Python
关于python pygame游戏进行声音添加的技巧
2021/10/24 Python