PHP sprintf Function Usage Example Tutorial

Source: Internet
Author: User
  1. 1. Percent: replace% to%
  2. $STR = ' Test the percent of this parameter, what will be replaced by ';
  3. echo sprintf ($STR);
  4. Return result: Test the% of this parameter, which will be replaced by what (percent is replaced by a%)
  5. 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
  6. $STR = ' parameter%b will be replaced with binary number ';
  7. $arg = ' 10 ';
  8. Echo sprintf ($str, $arg);
  9. Return Result: Parameter 1010 is replaced with binary number
  10. $arg = 10.23;
  11. Echo sprintf ($str, $arg);
  12. Return Result: Parameter 1010 is replaced with binary number
  13. $arg = ' abc ';
  14. Echo sprintf ($str, $arg);
  15. Return Result: Parameter 0 is replaced with binary number
  16. 3. %c returns the ASCII code for character encoding
  17. $arg = 65;
  18. $str = "Number {$arg} corresponding ASCII code is%c";
  19. Echo sprintf ($str, $arg);
  20. Return result: The ASCII code for the number 65 is a
  21. 4. %d replaces a segment of character%d with the type int, the data requires 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 4
  29. $arg = ' abc ';
  30. Echo sprintf ($str, $arg);
  31. Return Result: ID number 0
  32. 5. %s-String
  33. $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 ";
  34. $arg = '%s ';
  35. Echo sprintf ($str, $arg, 6,5);
  36. 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 Code

For 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:

    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 (All-in-a-
Copy Code

Update 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:

    1. For example, the value corresponding to the ID is the following array
    2. $info = Array (1=> ' Zhang San ',2=> ' John Doe ',3=> ' Harry ');
    3. $ids = Implode (', ', Array_keys ($info))//Get 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)
Copy Code

The above can complete the bulk update operation where the WHERE clause ensures that only 3 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.