Phpsprintf function usage example tutorial

Source: Internet
Author: User
Phpsprintf function usage example tutorial

  1. // 1. %: replace % with %
  2. $ Str = 'Test %, which will be replaced ';
  3. Echo sprintf ($ str );
  4. // Return result: Test the % parameter, and replace it with % (% is replaced with %)
  5. // 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.
  6. $ Str = 'parameter % B will be replaced with the binary number ';
  7. $ Arg = '10 ';
  8. Echo sprintf ($ str, $ arg );
  9. // Return result: the value 1010 is replaced with the binary number.
  10. $ Arg = 10.23;
  11. Echo sprintf ($ str, $ arg );
  12. // Return result: the value 1010 is replaced with the binary number.
  13. $ Arg = 'abc ';
  14. Echo sprintf ($ str, $ arg );
  15. // Return result: the value 0 is replaced with the 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 ASCII code corresponding to the number 65 is.
  21. // 4.% d replace % d in a character segment with the int type. the data requirements are the same as those of $ B.
  22. $ Str = 'Id = % d ';
  23. $ Arg =-3;
  24. Echo sprintf ($ str, $ arg );
  25. // Return result: ID is-3
  26. $ Arg = 4.5;
  27. Echo sprintf ($ str, $ arg );
  28. // Return result: ID 4
  29. $ Arg = 'abc ';
  30. Echo sprintf ($ str, $ arg );
  31. // Return result: ID 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. 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 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:

  1. UPDATA table
  2. SET field = CASE id
  3. WHEN 1 THEN 'value1'
  4. WHEN 2 THEN 'value2'
  5. WHEN 3 THEN 'value3'
  6. END
  7. 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:

  1. // For example, the value corresponding to the id is the following array
  2. $ Info = array (1 => 'Zhang San', 2 => 'Li Si', 3 => 'Wang Wu ');
  3. $ Ids = implode (',', array_keys ($ info) // Obtain all ID strings
  4. // Combined SQL
  5. $ SQL = "UPDATA user SET username = CASE id ";
  6. Foreach ($ info as $ id => $ username ){
  7. $ SQL. = sprintf ("WHEN % d THEN % s", $ id, $ username );
  8. }
  9. $ SQL. = "END WHERE id IN ($ ids )";
  10. // $ Model-> query ($ SQL)

The above steps allow batch update. The where clause ensures that only three rows of data are executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.