PHP sprintf () function application (Definition and usage)

Source: Internet
Author: User
Document directory
  • Syntax
  • Description

Syntax
sprintf(format,arg1,arg2,arg++)
Parameters Description
Format Required. Conversion format.
Arg1 Required. Specifies the parameter inserted at the first % symbol in the format string.
Arg2 Optional. Specifies the parameter inserted at the second % symbol in the format string.
Arg ++ Optional. Specifies the parameters inserted to the third, fourth, and other % symbols in the format string.
Description

ParametersFormatIs the conversion format, starting from the percent sign ("%") to the end of the conversion character. PossibleFormatValue:
%-Percentage sign returned
% B-binary number
% C-Characters Based on ASCII values
% D-Signed decimal number
% E-resumable counting (for example, 1.5e + 3)
% U-Unsigned decimal number
% F-floating point number (local settings aware)
% F-floating point number (not local settings aware)
% O-octal values
% S-string
% X-hexadecimal (lowercase letter)
% X-hexadecimal (uppercase letters)
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.

Tips and comments
Note: If the % symbol is greater than the arg parameter, you must use a placeholder. The placeholder is inserted after the % symbol, which consists of numbers and "\ $. See example 3.

Example
Example 1

Copy codeThe Code is as follows :?
<? Php
$ Str = "Hello ";
$ Number = 123;
$ Txt = sprintf ("% s world. Day number % u", $ str, $ number );
Echo $ txt;
?>

Output:

Hello world. Day number 123

Example 2Copy codeThe Code is as follows: <? Php
$ Number = 123;
$ Txt = sprintf ("% f", $ number );
Echo $ txt;
?>

 

Output:

123.000000

Example 3Copy codeThe Code is as follows: <? Php
$ Number = 123;
$ Txt = sprintf ("With 2 decimals: % 1 \ $. 2f <br/> 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 4Copy codeThe Code is as follows: <? Php
$ 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

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.