In PHP, There are many ways to format strings. According to the formatting type, it can be divided into string formatting and numeric character formatting. Numeric character formatting is the most commonly used. .
Let’s first explain the number_format() function in detail about the number character formatting function.
number_format() function definition is used to format numeric strings.
The syntax format is as follows:
number_format(number,decimals,decimalpoint,separator)
Parameter description: The number_format() function can have 1, 2 or 4 parameters, but not 3 parameters. If there is only one parameter, number, the value after the decimal point will be discarded after formatting, and the third digit will be separated by a comma (,); if there are two parameters, number will be formatted to the decimals digit after the decimal point. , decimalpoint is used to replace the decimal point (.), and separator is used to replace the comma (,) that separates the third digit.
Use the number_format() function to format the specified number string. The sample code is as follows:
<?php $number = 6666.88; echo number_format($number); //输出格式化后的数字字符串 echo "<br>"; echo number_format($number,2); //输出格式化后的数字字符串 echo "<br>"; $number1 = 223344.556677; echo number_format($number1,2,'.','.'); //输出格式化后的数字字符串 ?>
The output result is:
6,667
6,666.88
##223.344.56
A brief introduction to the string case conversion function
PHP provides 4 string case conversion functions. They all have only one optional parameterstring, which is to pass in the characters to be converted. string. Here we make a brief introduction, you can directly use these functions to complete the case conversion operation. Function strtoupper() is used to convert all given strings to uppercase letters; function strtolower() is used to convert all given strings to lowercase letters; function ucfirst() is used to convert the first letter in a given string to uppercase, leaving the remaining characters unchanged; the function ucwords() is used to convert all characters starting with The first letter of a space-delimited word is converted to uppercase. The following program is the usage code of these functions, as shown below:
<?php $lamp = "lamp is composed of Linux 、Apache、MySQL and PHP"; echo strtolower($lamp); echo strtoupper($lamp); echo ucfirst($lamp); echo ucwords($lamp); ?>
lamp is composed of linux, apache, mysql and php
LAMP IS CONPOSED OF LINUX, APACHE, MYSQL AND PHP
These functions just work as described in their instructions. To ensure that the first letter of a string is an uppercase letter and the rest are lowercase letters, you need to use matching methods. As shown below:
<?php $lamp = "lamp is composed of Linux 、Apache、MySQL and PHP"; echo ucfirst(strtolower($lamp)); ?>
The above is the detailed content of PHP format string function example usage introduction. For more information, please follow other related articles on the PHP Chinese website!