An in-depth analysis of the usage and difference _php examples of sprintf and printf functions in PHP

Source: Internet
Author: User
Tags sprintf string format

PHP sprintf () function

Replace the percent sign (%) symbol with a variable that is passed as a parameter:

<?php
$number = 2;
$STR = "Shanghai";
$txt = sprintf ("There are%u million cars in%s.", $number, $str);
echo $txt;
? >

Definitions and usage

The sprintf () function writes the formatted string to the variable.

The Arg1, arg2, + + parameters are inserted into the main string at the percent sign (%) symbol. The function is executed incrementally. At the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.

Note: If the% symbol is greater than the ARG argument, you must use a placeholder. The placeholder is after the% symbol and consists of numbers and "\$."

Grammar

Parameters Description
Format

is required. Specify the string and how to format the variables therein.

Possible format values:

  • %-Returns a percent semicolon%
  • %b-binary number
  • %c-ascii value corresponding to Character
  • %d-contains positive and negative decimal numbers (negative, 0, positive)
  • %e-scientific notation using lowercase (for example, 1.2e+2)
  • % E-use uppercase scientific notation (for example, 1.2E+2)
  • %u-unsigned decimal (greater than or equal to 0)
  • %f-floating-point number (local setting)
  • % F-floating point (not local setting)
  • %g-shorter%e and% F
  • % G-shorter%E and%f
  • %o-eight binary
  • %s-string
  • %x-16 binary (Small letter)
  • % x -16 binary (capital letter)

Additional format value. Must be placed between% and letters (for example,%.2f):

  • + (plus + or-before the number) to define the positive and negative of the number. By default, only negative numbers are marked and positive numbers are not marked)
  • ' (Specify what to use as a fill, and the default is a space.) It must be used in conjunction with the width designation. For example:% ' x20s (with "x" as padding)
  • -(left-resize variable value)
  • [0-9] (Specify the minimum width of the variable value)
  • . [ 0-9] (Specify number of decimal digits or maximum string length)

comments: If you use more than one of the above format values, they must be used in the order listed above.

Arg1 Necessary. Specify the parameter to be inserted into the first% symbol in the format string.
Arg2 Optional. Specify the parameter to be inserted into the second% symbol in the format string.
arg++ Optional. Specify the parameters to be inserted into the format string at the third to fourth% symbol.

PHP printf () function

Output formatted string:

<?php
$number = 9;
$str = "Beijing";
printf ("has%u millions bikes at%s.") ", $str, $number);
? >

Definitions and usage

The printf () function outputs a formatted string.

Grammar

printf (format,arg1,arg2,arg++)

Parameters Description
Format

is required. Specify the string and how to format the variables therein.

Possible format values:

  • %-Returns a percent semicolon%
  • %b-binary number
  • %c-ascii value corresponding to Character
  • %d-contains positive and negative decimal numbers (negative, 0, positive)
  • %e-scientific notation using lowercase (for example, 1.2e+2)
  • % E-use uppercase scientific notation (for example, 1.2E+2)
  • %u-unsigned decimal (greater than or equal to 0)
  • %f-floating-point number (local setting)
  • % F-floating point (not local setting)
  • %g-shorter%e and% F
  • % G-shorter%E and%f
  • %o-eight binary
  • %s-string
  • %x-16 binary (Small letter)
  • % x -16 binary (capital letter)

Additional format value. Must be placed between% and letters (for example,%.2f):

  • + (plus + or-before the number) to define the positive and negative of the number. By default, only negative numbers are marked, positive numbers are not marked)
  • ' (Specify what to use as a fill, and the default is a space.) It must be used in conjunction with the width designation.
  • -(left-resize variable value)
  • [0-9] (Specify the minimum width of the variable value)
  • . [ 0-9] (Specify decimal digits or maximum string length)

comments: If you use more than one of these format values, they must be used in the order listed above and cannot be disturbed.

Arg1 Necessary. Specify the parameter to be inserted into the first% symbol in the format string.
Arg2 Necessary. Specify the parameter to be inserted into the second% symbol in the format string.
arg++ Optional. Specify the parameters to be inserted into the format string at the third to fourth and so% symbols.

Here's an example: rounding to keep the two digits after the decimal point

<?php
$num 1 =;
Echo sprintf ("%0.2f", $num 1). " <br/> "; Output 21.00
$num 2 = 16.3287;
Echo sprintf ("%0.2f", $num 2). " <br/> "; Output 16.33
$num 3 = 32.12329;
Echo sprintf ("%0.2f", $num 3). " <br/> "; Output 32.12 

Explain the meaning of the%0.2f:

% indicates starting character
0 means the vacancy is filled with 0.
2 indicates that two digits must be taken after the decimal point
f indicates conversion to floating-point number

Convert character

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% print percent symbol, do not convert.
The b integer is converted into binary.
The c integer is converted to the corresponding ASCII character.
The d integer is turned into 10.
The F times the precision number is converted to floating point numbers.
The o integer is converted into octal.
The s integer turns into a string.
The x integer is converted to lowercase 16.
The X integer is converted to uppercase 16.

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.

Format the text after the output, such as:

$name = "Hunte"; 
$age = 25; 

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 the formatted text, the rest is the same as printf.

3. Print function:

is a function that can return a value and can have only one argument.

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.