Differences between sprintf and printf function usage in PHP

Source: Internet
Author: User
Here's an example: rounding to keep two decimal places

 
  "; Output 21.00$num2 = 16.3287;echo sprintf ("%0.2f", $num 2). "
"; Output 16.33$num3 = 32.12329;echo sprintf ("%0.2f", $num 3). "
"; Output 32.12?>

Explain the meaning of the following%0.2f:

% indicates the starting character
0 means the vacancy is filled with 0
2 indicates that the decimal point must occupy two digits
f = Convert to floating point number


Convert characters
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% printed percent symbol, not converted.
b integer turns into binary.
The c integer is converted to the corresponding ASCII character.
D integers are turned into 10.
The F-Times precision number is converted to floating point numbers.
o integers are turned into octal.
The s integer is converted into a string.
The x integer is converted to lowercase 16 rounding.
X integers are converted to uppercase 16 rounding.

The difference between printf and sprintf

1. printf function:

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

Produces output according to format, which are described in the documentation for SPRINTF ().

Returns the length of the outputted string.

The text is formatted for later output, such as:

$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 does not print, but returns formatted text, the rest is the same as printf.


3. Print function:

is a function that returns a value that can have only one parameter.

int print (string arg)

Outputs Arg. Returns 1, always.

The above describes the PHP sprintf and printf function usage differences, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.