Application of PHP sprintf () function (definition and usage) _php tutorial

Source: Internet
Author: User

Grammar

sprintf (format,arg1,arg2,arg++)
Parameters Description
Format Necessary. Conversion format.
Arg1 Necessary. Specifies the parameter that is inserted into the first% symbol in the format string.
Arg2 Optional. Specifies the parameter that is inserted into the second% symbol in the format string.
arg++ Optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol.

Description

Parametersformatis the converted format, starting with the percent sign ("%") to the end of the converted character. The following possibleformatValue:
Percent-return percentage symbol
%b-Binary number
%c-Characters in accordance with ASCII values
%d-Signed decimal number
%e-Sustainable counting method (e.g. 1.5e+3)
%u-Unsigned decimal number
%f-Floating point (Local settings Aware)
%F-Floating point number (not local settings aware)
%o-Eight binary number
%s-String
%x-16 binary number (lowercase letters)
%x-16 decimal digits (uppercase letters)
Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on

Hints and Notes
Note: If the% symbol is more than the arg parameter, you must use a placeholder. The placeholder is inserted after the% symbol and consists of a number and a "\$". See Example 3.

Example
Example 1
Copy CodeThe code is as follows:
?
$str = "Hello";
$number = 123;
$txt = sprintf ("%s World". Day number%u ", $STR, $number);
Echo $txt;
?>

Output:

Hello World. Day Number 123

Example 2
Copy CodeThe code is as follows:
$number = 123;
$txt = sprintf ("%f", $number);
Echo $txt;
?>
 

Output:

123.000000

Example 3
Copy CodeThe code is as follows:
$number = 123;
$txt = sprintf ("With 2 decimals:%1\$.2f
With no decimals:%1\ $u ", $number);
Echo $txt;
?>

Output:
Copy CodeThe code is as follows:
With 2 decimals:123.00
With no decimals:123

Example 4
Copy CodeThe code is as follows:
$ctype _primary = strtolower (' application ');
$ctype _secondary = strtolower (' pdf ');
$mimetype = sprintf ('%s/%s ', $ctype _primary, $ctype _secondary);
Echo $mimetype;
?>

Output:
Copy CodeThe code is as follows:
Application/pdf

http://www.bkjia.com/PHPjc/325510.html www.bkjia.com true http://www.bkjia.com/PHPjc/325510.html techarticle The Syntax sprintf (format,arg1,arg2,arg++) parameter describes the format required. Conversion format. Arg1 required. Specifies the parameter that is inserted into the first% symbol in the format string. Arg2 is optional. Regulations ...

  • 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.