In-depth understanding of phpprintf() output formatted string

高洛峰
Release: 2023-03-05 08:28:01
Original
1035 people have browsed it

php printf() 函数用于输出格式化的字符串,本文章向码农介绍php printf()函数的使用方法和基本使用实例,感兴趣的码农可以参考一下。

定义和用法

printf() 函数输出格式化的字符串。

arg1、arg2、arg++ 参数将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入到 % 符号之后,由数字和 "\$" 组成。请参见例子 2。

提示:相关函数:sprintf()、 vprintf()、 vsprintf()、 fprintf() 和 vfprintf()

•fprintf()
•sprintf()
•vfprintf()
•vprintf()
•vsprintf() 

语法

printf(format,arg1,arg2,arg++)
Copy after login

In-depth understanding of phpprintf() output formatted string

技术细节

In-depth understanding of phpprintf() output formatted string

实例

例子 1

使用格式值 %f:

Copy after login

例子 2

使用占位符:

没有小数:%1\$u",$number);
?>
Copy after login

例子 3

所有可能的格式值的演示:

",$num1); // 二进制数
printf("%%c = %c 
",$char); // ASCII 字符 printf("%%d = %d
",$num1); // 带符号的十进制数 printf("%%d = %d
",$num2); // 带符号的十进制数 printf("%%e = %e
",$num1); // 科学计数法(小写) printf("%%E = %E
",$num1); // 科学计数法(大写) printf("%%u = %u
",$num1); // 不带符号的十进制数(正) printf("%%u = %u
",$num2); // 不带符号的十进制数(负) printf("%%f = %f
",$num1); // 浮点数(视本地设置) printf("%%F = %F
",$num1); // 浮点数(不视本地设置) printf("%%g = %g
",$num1); // 短于 %e 和 %f printf("%%G = %G
",$num1); // 短于 %E 和 %f printf("%%o = %o
",$num1); // 八进制数 printf("%%s = %s
",$num1); // 字符串 printf("%%x = %x
",$num1); // 十六进制数(小写) printf("%%X = %X
",$num1); // 十六进制数(大写) printf("%%+d = %+d
",$num1); // 符号说明符(正) printf("%%+d = %+d
",$num2); // 符号说明符(负) ?>
Copy after login

例子 4

字符串说明符的演示:

",$str1);
printf("[%8s]
",$str1); printf("[%-8s]
",$str1); printf("[%08s]
",$str1); printf("[%'*8s]
",$str1); printf("[%8.8s]
",$str2); ?>
Copy after login

以上这篇深入理解php printf() 输出格式化的字符串就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHP中文网。

更多In-depth understanding of phpprintf() output formatted string相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!