This article mainly introduces the sprintf function usage in PHP, and analyzes the common usage of sprintf function formatting and output in the form of instances in detail, which has some reference value, for more information about sprintf functions in PHP, see the following example. Share it with you for your reference. The usage analysis is as follows:
The sprintf () function is officially used to format and output strings in php. This article will introduce some of your experiences in learning the sprintf () function, hope to help you.
The PHP function sprintf () is officially defined as: sprintf (): To Write formatted strings into a variable.
Syntax: sprintf (format, arg1, arg2, arg ++ );
Parameters:
Format: Required. conversion format
Arg1: Required. it specifies the parameter at the first % symbol in the format string to be inserted.
Arg1: (optional) specifies the parameter at the second % symbol in the format string to be inserted.
Arg1 ++: (optional) specifies the parameter at the third and fourth % symbols in the format string to be inserted.
The conversion format of the parameter format, starting with the percent sign (%) to the end of the conversion character. Below is a possible format value.
%-Percentage sign returned
% B-binary number
% C-characters based on ASCII values
% D-signed decimal number
% E-resumable counting (for example, 1.5e + 3)
% U-unsigned decimal number
% F-floating point number (local settings aware)
% F-floating point number (not local settings aware)
% O-octal values
% S-string
% X-hexadecimal (lowercase letter)
% X-hexadecimal (uppercase letters)
The following is a demo code:
The code is as follows:
// 1. %: replace % with %
$ Str = 'Test %, which will be replaced ';
Echo sprintf ($ str );
// Return result: Test the % parameter, and replace it with % (% is replaced with %)
// 2.% B: This parameter can only replace integer data. if it is a floating point type, only the integer part is taken, and the data after the decimal point is ignored. If the data type is not integer. Returns 0.
$ Str = 'parameter % B will be replaced with the binary number ';
$ Arg = '10 ';
Echo sprintf ($ str, $ arg );
// Return result: the value 1010 is replaced with the binary number.
$ Arg = 10.23;
Echo sprintf ($ str, $ arg );
// Return result: the value 1010 is replaced with the binary number.
$ Arg = 'abc ';
Echo sprintf ($ str, $ arg );
// Return result: the value 0 is replaced with the binary number.
// 3.% c returns the ASCII code of the character encoding
$ Arg = 65;
$ Str = "the ASCII code corresponding to the number {$ arg} is % c ";
Echo sprintf ($ str, $ arg );
// Return result: the ASCII code corresponding to the number 65 is.
// 4.% d replace % d in a character segment with the int type. the data requirements are the same as those of $ B.
$ Str = 'Id = % d ';
$ Arg =-3;
Echo sprintf ($ str, $ arg );
// Return result: ID is-3
$ Arg = 4.5;
Echo sprintf ($ str, $ arg );
// Return result: ID 4
$ Arg = 'abc ';
Echo sprintf ($ str, $ arg );
// Return result: ID 0
// 5.% s-string
$ Str = "this is the sprintf string (% s) used for testing ). I spent % f yuan today. There are % d stations from Bell Tower to Xiaozhai. Work ";
$ Arg = '% s ';
Echo sprintf ($ str, $ arg, 6, 5 );
// Return result: this is the sprintf string (% s) used for testing ). I spent 6.000000 yuan today. There are 5 sites from Bell Tower to Xiaozhai. Go to work
For other parameters, try to test.
The following describes some functions of this function. for example, when we update multiple fields for all data in a data table, it is very resource-consuming if we use cyclic update, here we will use the sprintf () function.
When the database is updated in batches, I generally use the case then when end syntax. the basic syntax is as follows:
The code is as follows:
UPDATA table
SET field = CASE id
WHEN 1 THEN 'value1'
WHEN 2 THEN 'value2'
WHEN 3 THEN 'value3'
END
WHERE id IN (1, 2, 3)
The above means that the value of id = 1 in the updated table is value1, the value of id = 2 is value2, and the value of id = 3 is value3, in this way, the function above the parameter combines the SQL statement into such an SQL statement, and only one SQL statement can be used for batch update. the specific method is as follows:
The code is as follows:
// For example, the value corresponding to the id is the following array
$ Info = array (1 => 'Zhang San', 2 => 'Li Si', 3 => 'Wang Wu ');
$ Ids = implode (',', array_keys ($ info) // Obtain all ID strings
// Combined SQL
$ SQL = "UPDATA user SET username = CASE id ";
Foreach ($ info as $ id => $ username ){
$ SQL. = sprintf ("WHEN % d THEN % s", $ id, $ username );
}
$ SQL. = "END WHERE id IN ($ ids )";
// $ Model-> query ($ SQL)
The above operation can be completed in batch update, and the where clause later ensures that only three rows of data are executed.
I hope this article will help you with PHP programming.