PHP self-_php tips for automatically filling digits or strings with functions

Source: Internet
Author: User

Let's take a look at an example: the requirement is to generate 4 digits, insufficient front complement 0

<?php  
//Generate 4 digits, insufficient front complement 0  
$var =sprintf ("%04d", 2);
echo $var//The result is 0002  
echo date (' Y_m_d ', Time ()). ' _ '. sprintf (' d ', rand (0,99));
? >

sprintf () function

Does it feel like a C language?

1. Grammar

sprintf (format,arg1,arg2,arg++)
Parameter description
Format required. Conversion format.
Arg1 required. 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 and so% symbols.

2. Notes

The parameter format is the format of the transformation, starting with the percent sign ("%") to the end of the converted character. The following possible format values:

%%-return percent symbol
%b-Binary number
%c-Characters with ASCII values
%d-Signed decimal digits
%e-Continuous counting method (e.g. 1.5e+3)
%u-Unsigned decimal digits
%f-floating-point number (Local settings Aware)
%F-floating-point number (not local settings aware)
%o-Eight binary number
%s-String
%x-16 Number (small letter)
%x-16 Number (capital letter)
Arg1, arg2, + + etc parameters will be inserted into the main string of the percent semicolon (%) Symbol Place. The function is executed incrementally. In the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on.

<?php  
$number = 123;  
$txt = sprintf ("%f", $number);  
echo $txt;  
? >

3. Format Digital Number_format ()

<?php  
$number = 1234.56;

中文版 notation (default)
$english _format_number = Number_format ($number);
1,235

//French notation
$nombre _format_francais = Number_format ($number, 2, ', ', ', ');
1 234,56

$number = 1234.5678;

中文版 notation without thousands seperator
$english _format_number = Number_format ($number, 2, '. ', ');
1234.57
?>

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.