In PHP, the sprintf function can write the formatted string into a variable for escape. The syntax is "sprintf(format, which stipulates that it is inserted into the first % symbol in the format string. Parameters, specify the parameters inserted at the second % symbol in the format string,...)", where format is used to specify the string and how to format the variables in it.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
sprintf() function writes a formatted string into a variable.
arg1, arg2, parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, arg2, and so on.
Note: If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % symbol and consists of a number and "\$".
The syntax is:
sprintf(format,arg1,arg2,arg++)
format required. Specifies a string and how to format variables within it.
Possible format values:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII 字符 50 是 2 // 注释:格式值 "%%" 返回百分号 echo sprintf("%%b = %b",$num1)."<br>"; // 二进制数 echo sprintf("%%c = %c",$char)."<br>"; // ASCII 字符 echo sprintf("%%d = %d",$num1)."<br>"; // 带符号的十进制数 echo sprintf("%%d = %d",$num2)."<br>"; // 带符号的十进制数 echo sprintf("%%e = %e",$num1)."<br>"; // 科学计数法(小写) echo sprintf("%%E = %E",$num1)."<br>"; // 科学计数法(大写) echo sprintf("%%u = %u",$num1)."<br>"; // 不带符号的十进制数(正) echo sprintf("%%u = %u",$num2)."<br>"; // 不带符号的十进制数(负) echo sprintf("%%f = %f",$num1)."<br>"; // 浮点数(视本地设置) echo sprintf("%%F = %F",$num1)."<br>"; // 浮点数(不视本地设置) echo sprintf("%%g = %g",$num1)."<br>"; // 短于 %e 和 %f echo sprintf("%%G = %G",$num1)."<br>"; // 短于 %E 和 %f echo sprintf("%%o = %o",$num1)."<br>"; // 八进制数 echo sprintf("%%s = %s",$num1)."<br>"; // 字符串 echo sprintf("%%x = %x",$num1)."<br>"; // 十六进制数(小写) echo sprintf("%%X = %X",$num1)."<br>"; // 十六进制数(大写) echo sprintf("%%+d = %+d",$num1)."<br>"; // 符号说明符(正) echo sprintf("%%+d = %+d",$num2)."<br>"; // 符号说明符(负) ?>
Recommended learning: "PHP Video Tutorial
"The above is the detailed content of How to escape sprintf function in php. For more information, please follow other related articles on the PHP Chinese website!