Go PHP Echo, print, printf, sprintf function differences and use

Source: Internet
Author: User
Tags clear screen sprintf

1. Echo Function:

The output function , which is a command, cannot return a value. Echo can be followed by a number of parameters, separated by semicolons, such as:

Echo $myvar 1  Echo ,$myvar, "<b>bold</b>";

2. print Function:

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

int string  1 , always.

3. printf function:

int string format [, mixed args [, mixed ...]]
Produces output according format to, which are described in the documentation for sprintf () .
//Returns The length of the outputted string.  

The text is formatted for later output, such as:

= "Hunte"$age  =printf$name$age);

4. sprintf function:

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

5. The printf () function is explained in detail:

printfThe call format for the () function is:printf("< formatted string >", < Parameter table >); % D Decimal signed integer% u decimal unsigned integer% F floating point number% s string% C single character%the value of the p pointer%floating-point numbers in e -exponential form%x, % X unsigned integer in hexadecimal notation% o unsigned integer represented in octal%gAutomatic selection of appropriate representations

Description

(1). You can insert a number between "%" and a letter to indicate the maximum field width.

① For example:%3d represents the output of 3-bit integers, not enough 3-bit right-justified.

The ②%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2, the integer digit is 6, the decimal point is one, and not enough 9-bit right-aligned.

The ③%8s represents the output of a 8-character string, which is not aligns aligned to 8 characters.

④ if the length of the string or the number of integers exceeds the width of the description, the output is based on its actual length.

⑤ floating point number, if the integer number of bits exceeds the width of the indicated integer digits, the actual integer digits will be output;

⑥ fractional digits exceed the indicated decimal width, the output is rounded by the width of the description.

⑦ If you want to add some 0 before the output value, add a 0 before the field width.

For example,%04d means that when you output a value that is less than 4 bits, it will be preceded by 0 to make its total width 4 bits.

⑧ If you use floating-point numbers to represent the output format of a character or integer, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.

For example,%6.9s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9, the contents of the 9th character will be deleted.

(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form.

① For example:%ld indicates an output long integer

②%LF indicates the output double floating-point number

(3). You can control the output left or right alignment, that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified, otherwise it is right-justified.

① For example:%-7d indicates output 7-bit integer left justified

②%-10s indicates output 10 characters aligns aligned

(4). Some special provisions of the character

   ①\ n     line    ②\f     Clear screen and change page    ③\ r     enter    ④ \ t     Tab Character    ⑤\xhh   represents an ASCII code with 16-in notation  ,    ⑥ where HH is 1 to 2 16 decimal

Attention:

<br/>: Wrappingin HTML, showing another line, viewing the source file in another line

\ n:the line break in PHP, the performance does not wrap, see the source file for a line

6. printf () : examples

Example 1: various examples

$n= 43951789; $u=-43951789; $c= 65;//ASCII-is ' a '//notice the double percent, this prints A literal '% ' characterprintf("%%b = '%b ' \ n",$n);//binary Representationprintf("%%c = '%c ' \ n",$c);//Print the ASCII character, same as Chr () functionprintf("%%d = '%d ' \ n",$n);//Standard integer representationprintf("%%e = '%e ' \ n",$n);//Scientific notationprintf("%%u = '%u ' \ n",$n);//unsigned integer representation of a positive integerprintf("%%u = '%u ' \ n",$u);//unsigned integer representation of a negative integerprintf("%%f = '%f ' \ n",$n);//floating point representationprintf("%%o = '%o ' \ n",$n);//octal representationprintf("%%s = '%s ' \ n",$n);//string Representationprintf("%%x = '%x ' \ n",$n);//hexadecimal representation (lower-case)printf("%%x = '%x ' \ n",$n);//hexadecimal representation (upper-case) printf("%%+d = '%+d ' \ n",$n);//Sign specifier on a positive integerprintf("%%+d = '%+d ' \ n",$u);//Sign specifier on a negative integerThe printout of this program would is:%b = '10100111101010011010101101'%c = 'A'%d = '43951789'%e = '4.39518e+7'%u = '43951789'%u = '4251015507'%f = '43951789.000000'%o = '247523255'%s = '43951789'%x = '29EA6AD'%x = '29EA6AD'%+d = '+43951789'%+d = '-43951789‘

Example 2: string Specifiers

$s= ' Monkey '; $t= ' Many monkeys '; printf("[%s]\n",$s);//Standard string Outputprintf("[%10s]\n",$s);//right-justification with Spacesprintf("[%-10s]\n",$s);//left-justification with Spacesprintf("[%010s]\n",$s);//zero-padding works on strings tooprintf("[% ' #10s]\n",$s);//Use the custom padding character ' # 'printf("[%10.10s]\n",$t);//Left-justification But with a cutoff of ten charactersThe printout of this program would is:[ monkey] [monkey] [monkey] [0000monkey] [## #monkey][Many Monke]

Example 3:zero-padded integers

$isodate sprintf $year $month $day);

Example 4:formatting currency

$money 1 = 68.75$money 2 = 54.35$money$money 1$money 2  //$formattedsprintf$money//

Example 5: sprintf () : Scientific notation

$number = 362525200echosprintf$number//

Go PHP Echo, print, printf, sprintf function differences and use

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.