函数sprintf()作用是什么
我的PHP手册中sprintf()的说明全是英文,我看不懂。有没有大侠能把该函数的作用(尤其是参数format)详细地说一下?
回复讨论(解决方案)
请 google 翻译了一下
% - 文字百分号。没有参数??是必需的。
b - 该参数被视为一个整数,并作为一个二进制数。
c - 接收参数被视为一个整数,并作为字符与ASCII值。
d - 该参数被视为一个整数,作为一个(符号)十进制数。
e - 该参数被视为科学记数法(比如1.5e +3 ) 。
u - 该参数被视为一个整数,并作为一个无符号十进制数。
f - 该参数被视为浮点,以及作为一个浮点数(语言环境感知) 。
F - 该参数被视为浮点,以及作为一个浮点数(非语言环境感知) 。从PHP 4.3.10和PHP 5.0.3 。
o - 该参数被视为一个整数,并作为一个八进制数。
s - 该参数被视为并作为一个字符串。
x - 将参数被视为一个整数,作为一个十六进制数(小写字母) 。
X- 将参数被视为一个整数,作为一个十六进制数(用大写字母) 。
例:
echo sprintf('%b %c %d %u %e %f %F %o %s %x %X', 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65);
1000001 A 65 65 6.500000e+1 65.000000 65.000000 101 65 41 41
是不是只有浮点型才会有“%.2f”的参数format?
是的
%.2f 表示按 2 位小数格式化
整数自然是没有小数的
sprintf() 函数把格式化的字符串写写入一个变量中。
sprintf(format,arg1,arg2,arg++)
参数
format 必需。转换格式。
arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。
arg2 可选。规定插到 format 字符串中第二个 % 符号处的参数。
arg++ 可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。
<?php$str = "Hello";$number = 123;$txt = sprintf("%s world. Day number %u",$str,$number);echo $txt;?>
输出:
Hello world. Day number 123
还有没有其他人补充了?
还有什么好补充的?
1. sprintf是 格式化输出,也就是按照你要的格式输出,类似模板
2. 与print不同,sprintf并不会直接打印到屏幕,因此你可以用它给变量赋值。如 $s = sprintf(..................);
w3school里sprintf()部分的一句话:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符插到 % 符号后面,由数字和 "\$" 组成。
上面这句话中的数字是指的什么?
这是 php 特有的一个方式,使用的并不多(他给的例子已经说得很清楚了)
这是正常的写法
$a = 123;$b = 1111;$txt = sprintf("%.2f %u",$a, $b);echo $txt;
当写作
$txt = sprintf("%2\$.2f %1\$u",$a, $b);echo $txt;
可以看到对应位置的值发生了变化
于是可知:数字表示的是参数的序号
非常感谢,结帖。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
