PHP 中的 sprintf
php中的sprintf是一个函数,用于将格式化字符串写入变量并返回格式化字符串。在PHP中,版本4及以上版本支持这个sprintf函数。 sprintf() 函数与 printf() 函数类似,但它们之间的主要且唯一的区别是 sprintf() 将输出保存到字符串中,而不是像 printf() 函数那样在浏览器上显示格式化输出。 sprintf() 函数可以与 echo 一起使用,即 sprintf() 返回的格式化字符串使用 echo 打印在浏览器上。让我们更深入地研究这个主题,看看它的语法、可访问的格式,并解决一些程序。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
这是 PHP 中 sprintf() 函数的语法。
sprintf(format, arg1, arg2, arg3, …….)
这里的arg1、arg2、arg3等是sprintf()的参数。 arg1 是必须插入到第一个参数的参数。 arg2, arg3, ……… 是要插入的可选参数。
Format:必填参数,指定如何格式化其中变量的字符串。
以下是可能的格式:
参数
Parameter |
Description |
%b | Argument present as a binary number |
%% | Returns % sign |
%d | Parameter treated as a positive integer, represented as a decimal number |
%c | Parameter treated as an integer, represented as a character with ASCII |
%e | Precision specifier that specifies the number of digits after the decimal point. Scientific notation with lowercase |
%u | Parameter treated as an integer, represented as unsigned integer |
%f | Floating-point number(locale) |
%g | General format |
%o | Represented as Octal number |
%x | Represented as Hexadecimal number with lowercase letters |
%s | Argument presented and treated as a string |
%E | Similar to %e specifier but with Uppercase. |
%F | Floating-point number(Non-locale) |
%G | Similar to %g specifier but uses %E and %F |
%X | Represented with Hexadecimal number but with uppercase |
描述
There are some additional format values, which are placed between % and letter.
- +, both + and – are forced in front of numbers. Negative numbers are marked by default.
- ‘ Specifies what is to be used as padding.
- – Left justifies the variable
- [0-9] Specifies minimum width held to the variable.
- .[0-9] Specifies the number of decimal digits or the maximum string length.
How does sprintf() function work in PHP?
Let us see How sprintf() function in PHP works with few examples,
Example #1
Code:
<!DOCTYPE html> <html> <body> <?php $num1 = 321234; $num2 = 860; $text = sprintf("%f,%f",$num1, $num2); echo $text; ?> </body> </html>
Output:
Here, we have taken two float values and using sprintf() function, scanned the variables, and using echo, have printed the floating values on the console.
Example #2: for floating point decimals
Code:
<!DOCTYPE html> <html> <body> <?php $num1 = 4563; $text = sprintf("With 3 decimals: %1\$.3f <br>With no decimals: %1\$u <br>With single decimal: %1\$.1f",$num1); echo $text; ?> </body> </html>
Output:
So here for floating values, we have specified as to no decimals, or single decimal, or 3 decimal values.
Example #3: with string specifiers
Code:
<!DOCTYPE html> <html> <body> <?php $string1 = 'PHPv4'; echo sprintf("[%s]",$string1)."<br>"; echo sprintf("[%08s]",$string1)."<br>"; echo sprintf("[%-8s]",$string1)."<br>"; echo sprintf("[%8s]",$string1)."<br>"; echo sprintf("[%8.8s]",$string1)."<br>"; echo sprintf("[%'*8s]",$string1)."<br>"; ?> </body> </html>
Output:
So based on the output, [%s] will return the string as it is
[%08s] will return string with zero padding [%-8s] will return string with left justification [%8s] will return string with the right justification [%8.8s] will return string with left justification, cuts of characters after a specific value [%’*8s] will return string with additional *Example #4: with Argument swapping
Code:
<!DOCTYPE html> <html> <body> <?php $num = 7; $fruits = 'Mangoes'; $arg1 = 'The %2$s are %1$d in number'; echo sprintf($arg1, $num, $fruits); ?> </body> </html>
Output:
So here, format string supports argument swapping/ numbering.
Imagine if the placeholders in format string do not match the order of the arguments as shown above. And hence, we have indicated the arg1 which arguments refer to which placeholders.
Example #5: for all format specifiers
Code:
<!DOCTYPE html> <html> <body> <?php $arg1 = 456; $arg2 = -456; $str = 57; echo sprintf("%%b = %b",$arg1)."<br>"; echo sprintf("%%d = %d",$arg1)."<br>"; echo sprintf("%%d = %d",$arg2)."<br>"; echo sprintf("%%c = %c",$str)."<br>"; echo sprintf("%%e = %e",$arg1)."<br>"; echo sprintf("%%u = %u",$arg1)."<br>"; echo sprintf("%%u = %u",$arg2)."<br>"; echo sprintf("%%f = %f",$arg1)."<br>"; echo sprintf("%%f = %f",$arg2)."<br>"; echo sprintf("%%g = %g",$arg1)."<br>"; echo sprintf("%%g = %g",$arg2)."<br>"; echo sprintf("%%o = %o",$arg1)."<br>"; echo sprintf("%%o = %o",$arg2)."<br>"; echo sprintf("%%x = %x",$arg1)."<br>"; echo sprintf("%%x = %x",$arg2)."<br>"; echo sprintf("%%s = %s",$arg1)."<br>"; echo sprintf("%%s = %s",$arg2)."<br>"; echo sprintf("%%E = %E",$arg1)."<br>"; echo sprintf("%%F = %F",$arg1)."<br>"; echo sprintf("%%G = %G",$arg1)."<br>"; echo sprintf("%%X = %X",$arg1)."<br>"; echo sprintf("%%+d = %+d",$arg1)."<br>"; echo sprintf("%%+d = %+d",$arg2)."<br>"; ?> </body> </html>
Output:
So here we have shown all the format specifiers.
With this, we shall conclude the topic ‘sprintf in php’. We have seen the syntax of sprintf() function in PHP. We have seen what each format specifier means and have Illustrated few examples on how to use sprintf in PHP. The above examples will give a clear understanding of all the format specifiers.
以上是PHP 中的 sprintf的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。
