Home > Backend Development > PHP Tutorial > PHP sprintf function usage example tutorial

PHP sprintf function usage example tutorial

WBOY
Release: 2016-07-25 08:52:32
Original
1781 people have browsed it
  1. // 1. %%: Replace %% with %
  2. $str = 'Test the parameter %% and see what it will be replaced with';
  3. echo sprintf($str);
  4. //Return the result : Test what the % parameter will be replaced with (%% is replaced by a %)
  5. // 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
  6. $str = 'Parameter %b will be replaced with a binary number';
  7. $arg = '10';
  8. echo sprintf($str,$arg);
  9. //Return result: Parameter 1010 will be replaced with a binary number
  10. $arg = 10.23;
  11. echo sprintf($str,$arg);
  12. //Return result: parameter 1010 will be replaced with a binary number
  13. $arg = 'abc';
  14. echo sprintf($str,$arg);
  15. / /Return result: Parameter 0 will be replaced with a binary number
  16. // 3. %c returns the ASCII code of the character encoding
  17. $arg = 65;
  18. $str = "The ASCII code corresponding to the number {$arg} is %c ";
  19. echo sprintf($str,$arg);
  20. //Return result: The number 65 corresponds to the ASCII code A
  21. // 4. %d replaces %d in a character with int type, the data requirements are the same as $b
  22. $str = 'ID number is %d ';
  23. $arg = -3;
  24. echo sprintf($str,$arg);
  25. //Return result: ID number is -3
  26. $arg = 4.5;
  27. echo sprintf ($str,$arg);
  28. //Return result: ID number is 4
  29. $arg = 'abc';
  30. echo sprintf($str,$arg);
  31. //Return result: ID number is 0
  32. / / 5. %s - String
  33. $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";
  34. $ arg = '%s';
  35. echo sprintf($str,$arg,6,5);
  36. //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'
  1. Where id in (1,2,3)
  2. Copy the code
  3. 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){
  1. $sql .= sprintf("WHEN %d THEN %s",$id,$username);
  2. }
  3. $sql .= "END WHERE id IN ($ids)";
  4. // $model->query($sql)
  5. Copy code
  6. The above can complete the batch update operation, where the where clause ensures that only 3 rows of data are executed.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template