- 1. Percent: replace% to%
- $STR = ' Test the percent of this parameter, what will be replaced by ';
- echo sprintf ($STR);
- Return result: Test the% of this parameter, which will be replaced by what (percent is replaced by a%)
- 2. %b: This parameter can only replace integer data, and 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 is non-integer. Returns 0
- $STR = ' parameter%b will be replaced with binary number ';
- $arg = ' 10 ';
- Echo sprintf ($str, $arg);
- Return Result: Parameter 1010 is replaced with binary number
- $arg = 10.23;
- Echo sprintf ($str, $arg);
- Return Result: Parameter 1010 is replaced with binary number
- $arg = ' abc ';
- Echo sprintf ($str, $arg);
- Return Result: Parameter 0 is replaced with binary number
- 3. %c returns the ASCII code for character encoding
- $arg = 65;
- $str = "Number {$arg} corresponding ASCII code is%c";
- Echo sprintf ($str, $arg);
- Return result: The ASCII code for the number 65 is a
- 4. %d replaces a segment of character%d with the type int, the data requires 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 4
- $arg = ' abc ';
- Echo sprintf ($str, $arg);
- Return Result: ID number 0
- 5. %s-String
- $str = "This is the string used to test the sprintf (%s). Today, we consumed%f yuan. From the clock tower to the small village there are%d stations. Work ";
- $arg = '%s ';
- Echo sprintf ($str, $arg, 6,5);
- Returns the result: This is the string used to test the sprintf (%s). Today, we spent 6.000000 yuan. There are 5 stops from the bell tower to the small village. Work
Copy CodeFor parameter usage, testing: When you are updating a data table with all the data in a single field, it is very resource-intensive if you use cyclic updating, and we need to use our sprintf () function. When a database is bulk updated, the syntax for the case and when end is generally used, and the basic syntax, such as:
- Updata table
- SET field = case ID
- When 1 Then ' value1 '
- When 2 Then ' value2 '
- When 3 Then ' Value3 '
- END
- WHERE ID in (All-in-a-
Copy CodeUpdate table Set id = 1 is value1, the value of id = 2 is value2, and the value of id = 3 is value3, so the function above the parameter combines SQL statements into such SQL statements, which can be batched updated with just one SQL Example:
- For example, the value corresponding to the ID is the following array
- $info = Array (1=> ' Zhang San ',2=> ' John Doe ',3=> ' Harry ');
- $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 CodeThe above can complete the bulk update operation where the WHERE clause ensures that only 3 rows of data are executed. |