- // 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 parameter usage, test: when updating multiple fields of all data in a data table, it is very resource-consuming if Cyclic update is used. here we will use the sprintf () function. When the database is updated in batches, the case then when end syntax is generally used. the basic syntax 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)
Update table to set the value of id = 1 to value1, the value of id = 2 to value2, and the value of id = 3 to 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.
Example:
- // 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 steps allow batch update. The where clause ensures that only three rows of data are executed. |