PHP function: sprintf () Learning Notes

Source: Internet
Author: User
Tags net return sprintf


The PHP function sprintf () function is officially defined as: sprintf (): Writes a format string to a variable

The syntax is:
sprintf (format,arg1,arg2,arg++);

Parameters:
Format: must, convert formatting
Arg1: Must, specify the parameter to insert the first% symbol in the format string
Arg1: Optional, specify the parameter to be inserted at the second% symbol in the format string
arg1++: Optional, specify the parameters to insert the third to fourth% symbol in the format string

The conversion format of the parameter format, starting with the percent sign (%) to the end of the converted character, with the following possible format values

%%– return percent symbol
%b– binary number
%c– characters according to ASCII values
%d– with signed decimal numbers
%e– continuous counting method (e.g. 1.5e+3)
%u– unsigned decimal digits
%f– floating-point number (Local settings Aware)
%f– floating-point number (not local settings aware)
Number of%o– octal
%s– string
%x– hexadecimal number (lowercase letter)
%x– hexadecimal number (capital letter)

Here are some demo:

The code is as follows Copy Code

1. %%: replace% of%
$STR = ' Test The percent% of this parameter, will be replaced by what ';
echo sprintf ($STR);
Return result: test% of this parameter, will be replaced by what (%% is replaced by a%)


2. %b: This parameter can only replace integer data and, if it is a floating-point type, takes only the integral part and ignores the data after the decimal point. If the data is not integral. Www.111Cn.net return 0
$STR = ' parameter%b will be replaced with binary number ';
$arg = ' 10 ';
Echo sprintf ($str, $arg);
Return Result: Parameter 1010 replaces binary number
$arg = 10.23;
Echo sprintf ($str, $arg);
Return Result: Parameter 1010 replaces binary number
$arg = ' abc ';
Echo sprintf ($str, $arg);
Return Result: Parameter 0 replaces binary number

3. %c returns the ASCII code of the character encoding
$arg = 65;
$str = "Number {$arg} corresponds to the ASCII code is%c";
Echo sprintf ($str, $arg);
Return Result: The number 65 corresponds to the ASCII code is a

4. %d replaces a section of character%d with an int with the same data requirements 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);
Www.111Cn.net return Result: ID number is 0

5. %s-String
$str = "This is the sprintf string (%s) used to test. Today,%f yuan was consumed. There are%d stops from the bell tower to the small village. Work ";
$arg = '%s ';
Echo sprintf ($str, $arg, 6,5);
Return result: This is the sprintf string (%s) used to test. We spent 6.000000 yuan today. There are 5 stops from the bell tower to the small village. Work

As for the other parameters. You can try the test.

Here are some of the uses of this function. For example, when we are updating multiple fields for all data in a datasheet. If you use a cyclic update. That is very resource-intensive. We're going to use our sprintf () function here.
When the database is batch updated. I usually do this by using the syntax of case then. Basic syntax such as:

The code is as follows Copy Code

Updata table
SET field = case ID
When 1 THEN ' value1 '
When 2 THEN ' value2 '
When 3 THEN ' Value3 '
End
WHERE ID in (1,2,3)

The meaning of the above means. Update table Setting ID = 1 has a value of value1, the value of id = 2 is value2, and the value of id = 3 is value3. This allows the function above the parameter to combine the SQL statements into such SQL statements. A batch update is possible with just one SQL. The specific methods are:

The code is as follows Copy Code
For example, the corresponding value of ID is the following array
$info = Array (1=> ' John ',2=> ' Dick ',3=> ' Harry ');
$ids = Implode (', ', Array_keys ($info))//Get all ID strings
Combining 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)

The above can complete the batch update operation. The WHERE clause that follows 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.