Home > Backend Development > PHP Tutorial > PHP character formatting

PHP character formatting

韦小宝
Release: 2023-03-20 22:40:02
Original
5482 people have browsed it

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() // 函数把格式化的字符串写入一个变量中。
Copy after login

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 //十六进制数(大写字母)
Copy after login

Code case:

<?php
$str = "Hello";
$number = 123;
$txt = sprintf("%s world.  number %d",$str,$number);
echo $txt;
?>
Copy after login

Code result:

Hello world.  number 123
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template