-
- // 1. %%: Replace %% with %
- $str = 'Test the parameter %% and see what it will be replaced with';
- echo sprintf($str);
- //Return the result : Test what the % parameter will be replaced with (%% is replaced by a %)
- // 2. %b: This parameter can only replace integer data. If it is a floating point type, only the integer part will be taken. Data after the decimal point will be ignored. If it is non-integer data. Return 0
- $str = 'Parameter %b will be replaced with a binary number';
- $arg = '10';
- echo sprintf($str,$arg);
- //Return result: Parameter 1010 will be replaced with a binary number
- $arg = 10.23;
- echo sprintf($str,$arg);
- //Return result: parameter 1010 will be replaced with a binary number
- $arg = 'abc';
- echo sprintf($str,$arg);
- / /Return result: Parameter 0 will be replaced with a 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 number 65 corresponds to the ASCII code A
-
- // 4. %d replaces %d in a character with int type, the data requirements are the same as $b
- $str = 'ID number is %d ';
- $arg = -3;
- echo sprintf($str,$arg);
- //Return result: ID number is -3
- $arg = 4.5;
- echo sprintf ($str,$arg);
- //Return result: ID number is 4
- $arg = 'abc';
- echo sprintf($str,$arg);
- //Return result: ID number is 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. Go to 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 stops from Bell Tower to Xiaozhai. Go to work
Copy the code
Regarding parameter usage, test:
When updating multiple fields of all the data in a data table, if circular updates are used, it will be very resource-intensive. Our sprintf() function will be used here.
When updating the database in batches, the syntax of case then when end is generally used. The basic syntax is, for example:
AUpdata Table Set Field = CASE ID When 1 Then 'Value1' WHEN 2 THEN 'Value2' 3 Then 'Value3' - Where id in (1,2,3)
-
-
- Copy the code
-
-
- Update the table and 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 parameters combines the sql statement into such a SQL statement. Only one SQL can perform batch updates
-
Example:
//For example, the value corresponding to id is the following array$info = array(1=>'Zhang San',2=>'Li Si',3=>'Wang Wu'); $ids = implode(',',array_keys($info)) //Get 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)
-
-
- Copy code
-
-
- The above can complete the batch update operation, where the where clause ensures that only 3 rows of data are executed.
-
|