In PHP development, we may encounter situations where adding double quotes to a line of text makes that line of text become a string. If other characters are needed in a line of text, we use double quotes. Or single quotes are very prone to errors, so today we will talk about how PHP formats characters!
Fortunately, our PHP has functionYou can use PHP directly sprintf() function
printf // 输出格式化字符串 sprintf() // 函数把格式化的字符串写入一个变量中。
PHP sprintf() function is used The parameters are as follows:
%% //返回百分比符号 %b //二进制数 %c //依照 ASCII 值的字符 %d //带符号十进制数 %e //可续计数法(比如 1.5e+3) %u //无符号十进制数 %f //浮点数(local settings aware) %F //浮点数(not local settings aware) %o //八进制数 %s //字符串 %x //十六进制数(小写字母) %X //十六进制数(大写字母)
Code case:
<?php $str = "Hello"; $number = 123; $txt = sprintf("%s world. number %d",$str,$number); echo $txt; ?>
Code result:
Hello world. number 123
is very simple and easy to understand Yes, please read more and then write some directly. You will find that it is really convenient!
Related recommendations:
Summary of 10 tutorials related to PHP formatting code
The above is the detailed content of PHP character formatting. For more information, please follow other related articles on the PHP Chinese website!