Detailed Explanation of sprintf Function Usage of PHP

  • 2021-08-03 09:28:38
  • OfStack

This paper illustrates the usage of sprintf function in PHP. Share it for your reference. The specific usage analysis is as follows:

sprintf () function in php official is to say that the string format output, this article to give you friends to introduce 1 in learning sprintf () function 1 experience sharing, I hope to give you help.

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: Required, Transform Format

arg1: Required, specifying the parameter inserted at the first% sign in the format string

arg1: Optional, specifying the parameter inserted at the 2nd% sign in the format string

arg 1 + +: Optional, specifying the parameters to be inserted in the format string at the% sign such as 3. 4

The conversion format for the parameter format starts with the percentage sign (%) and ends with the conversion character. Here are the possible format values.

%% returns the percentage symbol

% b binary number

% c characters according to ASCII values

% d signed decimal number

% e continuous enumeration (e.g. 1.5 e+3)

% u unsigned decimal number

% f Floating Point Number (local settings aware)

% F Floating Point Number (not local settings aware)

% o octal number

% s string

% x 106 binary numbers (lowercase letters)

% X 106 binary numbers (uppercase letters)

Here are some demo with the following code:

// 1. %% : Put  %%  Replace with  %  
$str = ' Test 1 Under %% What will this parameter be replaced with '; 
echo sprintf($str); 
// Return results: Test 1 Under % What will this parameter be replaced with (%% Be replaced with 1 A %)  // 2. %b : This parameter can only replace integer data. If it is floating-point, only the integer part will be taken, and the data after the decimal point will be ignored. If non-integer data. Return 0 
$str = ' Parameter %b Will be replaced by 2 Binary number '; 
$arg = '10'; 
echo sprintf($str,$arg); 
// Return result: Parameters 1010 Will be replaced by 2 Binary number  
$arg = 10.23; 
echo sprintf($str,$arg); 
// Return result: Parameters 1010 Will be replaced by 2 Binary number  
$arg = 'abc'; 
echo sprintf($str,$arg); 
// Return result: Parameters 0 Will be replaced by 2 Binary number   // 3. %c Returns the character-encoded ASCII Code  
$arg = 65; 
$str =  " Figures {$arg} Corresponding ASCII Code is %c "; 
echo sprintf($str,$arg); 
// Return results: Numbers 65 Corresponding ASCII Code is A 
 
// 4. %d Will 1 In segment characters %d Replace with int Type , Data requirements are the same as those of $b Same  
$str = 'ID Number is %d '; 
$arg = -3; 
echo sprintf($str,$arg); 
// Return results: ID Number is -3 
$arg = 4.5; 
echo sprintf($str,$arg); 
// Return results: ID Number is 4 
$arg = 'abc'; 
echo sprintf($str,$arg); 
// Return results: ID Number is 0 
 
// 5. %s - String  
$str = " This is for testing sprintf String of ( %s ) . I spent today %f Yuan. There are from the bell tower to Xiaozhai %d Stand. Go to work "; 
$arg = '%s'; 
echo sprintf($str,$arg,6,5); 
// Return results: This is for testing sprintf String of ( %s ) . I spent today 6.000000 Yuan. There are from the bell tower to Xiaozhai 5 Stand. Go to work

As for other parameters, you can try to test 1.

Let's talk about some uses of this function below. For example, when we update multiple fields of all data in a data table, if we use circular update, it will consume resources, so we will use our sprintf () function here.

In the database batch update, I use case then when end syntax to do, basic syntax such as:

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)

That is to say, when updating table, the value of id = 1 is set as value1, the value of id = 2 is value2, and the value of id = 3 is value3. In this way, the function above parameters combines sql statements into such SQL statements, and only one SQL can be updated in batches. The specific method is as follows:
// For example  id  The corresponding values are the following array   
$info = array(1=>' Zhang 3',2=>' Li 4',3=>' Wang 5'); 
$ids = implode(',',array_keys($info)) // Gets all of the ID String  
// Combination 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 bulk update operation can be completed above, and the following where clause ensures that only 3 rows of data are executed.

I hope this article is helpful to everyone's PHP programming.


Related articles: