PHP sprintf function Usage detailed _php skill

Source: Internet
Author: User
Tags sprintf string format

This example describes the use of sprintf functions in PHP. Share to everyone for your reference. The specific usage analysis is as follows:

sprintf () function in the PHP official is said that the string format output, this article will give you some friends in learning sprintf () function of some experience to share, hope to bring help to everyone.

The PHP function sprintf () function is officially defined as: sprintf (): Writes a formatted 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 code as follows:
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. 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);
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 to test them.

Here are some of the uses of this function, such as we are in a data table for all the data on a field update, if the use of cyclic updates, it is very resource-intensive, we need to use our sprintf () function.

In the database batch update, I generally use the case then the syntax of the end of the grammar, the basic syntax such as:

Copy Code code as follows:
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 above means that the update table setting ID = 1 value is value1, the value of id = 2 is value2, the value of id = 3 is value3, so the function above the parameter combines SQL statements into such SQL statements, and can be updated in batches with only one SQL, specifically The method is:
Copy Code code as follows:
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)

You can complete the bulk update operation, followed by the WHERE clause to ensure that only 3 rows of data are executed.

I hope this article will help you with your PHP program design.

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.