我的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;123.00 1111
当写作
$txt = sprintf("%2\$.2f %1\$u",$a, $b);echo $txt;1111.00 123
可以看到对应位置的值发生了变化
于是可知:数字表示的是参数的序号
非常感谢,结帖。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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