Differences between sprintf and printf functions in php

Source: Internet
Author: User

Differences between sprintf and printf functions in php

This article mainly introduces the difference between sprintf and printf functions in php. For more information, see

The following is an example: Rounding to the last two decimal places

 

The Code is as follows:

<? Php

$ Num1 = 21;

Echo sprintf ("% 0.2f", $ num1). "<br/>"; // output 21.00

$ Num2 = 16.3287;

Echo sprintf ("% 0.2f", $ num2). "<br/>"; // output 16.33

$ Num3 = 32.12329;

Echo sprintf ("% 0.2f", $ num3). "<br/>"; // output 32.12

?>

 

 

 

Explain the meaning of % 0.2f:

 

% Indicates the start character.

0 indicates that the vacancy is filled with 0

2 indicates two digits after the decimal point

F indicates converting to a floating point number.

 

 

Conversion character

=-

% Indicates the percentage, which is not converted.

The integer B is converted into binary.

Convert an integer in c to an ASCII character.

D integer to decimal place.

F times the precision number to the floating point number.

O integer to octal.

The s integer is converted into a string.

Convert an integer to a lowercase hexadecimal value.

Convert X to uppercase hexadecimal.

 

Differences between printf and sprintf

 

1. printf function:

 

Int printf (string format [, mixed args [, mixed...])

 

Produces output according to format, which is described in the documentation for sprintf ().

 

Returns the length of the outputted string.

 

Format the text and output it, for example:

 

The Code is as follows:

$ Name = "hunte ";

$ Age = 25;

Printf ("my name is % s, age % d", $ name, $ age );

 

 

2. sprintf function:

String sprintf (string format [, mixed args [, mixed...])

 

Returns a string produced according to the formatting string format.

 

Similar to printf, but not printed, the formatted text is returned. Others are the same as printf.

 

3. print function:

 

Yes. One value can be returned and only one parameter is allowed.

 

Int print (string arg)

 

Outputs arg. Returns 1, always.

 

 

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.