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 print (string arg)
Outputs arg . Returns 1 , always.
3. printf function:
int printf (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 formatted after the output, such as: $name = "Hunte"; $age = 25; printf ("My name is%s, age%d", $name, $age);
4. 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 formatted text, the rest is the same as printf.
5. The printf () function is explained in detail:
The printf () function is called in the format: printf ("< formatted string >", < Parameter table >);
%d decimal signed integer%u decimal unsigned integer%f floating-point number%s string%c single character%p pointer value%e exponential floating point%x,%x unsigned integer in hexadecimal%o unsigned integer%g in octal to automatically select an appropriate representation
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 carriage return ④\t tab character ⑤\XHH represents an ASCII code with 16-in notation, ⑥ where HH is 1 to 2 16 decimal
6. printf () : examples
Example 1: various examples
<?php $n = 43951789; $u =-43951789; $c = 65; ASCII is ' A '//notice the double percent, this prints A literal '% ' character printf ("%%b = '%b ' \ n", $n); Binary representation printf ("%%c = '%c ' \ n", $c); Print the ASCII character, same as Chr () function printf ("%%d = '%d ' \ n", $n); Standard integer Representation printf ("%%e = '%e ' \ n", $n); Scientific notation printf ("%%u = '%u ' \ n", $n); unsigned integer representation of a positive integer printf ("%%u = '%u ' \ n", $u); unsigned integer representation of a negative integer printf ("%%f = '%f ' \ n", $n); Floating point representation printf ("%%o = '%o ' \ n", $n); Octal representation printf ("%%s = '%s ' \ n", $n); String representation printf ("%%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 integer printf ("%%+d = '%+d ' \ n", $u); Sign specifier on a Negative integer?> The printout of this program would be:%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
<?php$s = ' monkey '; $t = ' many monkeys '; printf ("[%s]\n", $s);//Standard string output printf ("[%10s]\n", $s);//right-justification with spaces printf ("[%-10s]\n", $s);//left-justification with spaces printf ("[%010s]\n", $s);// Zero-padding works on strings too printf ("[% ' #10s]\n", $s);//Use the custom padding character ' # ' printf ("[%10.10s]\n", $t); Left-justification but with a cutoff of ten characters?> the printout of this program would be: [monk EY] [ monkey] [ monkey ] [0000monkey] [# # #monkey] [many Monke]
Example 3:zero-padded integers
<?php $isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day); ? >
Example 4:formatting currency
<?php $money 1 = 68.75; $money 2 = 54.35; $money = $money 1 + $money 2; echo $money would output "123.1"; $formatted = sprintf ("%01.2f", $money); echo $formatted would output "123.10" ?>
Example 5: sprintf () : Scientific notation
<?php $number = 362525200; Echo sprintf ("%.3e", $number); Outputs 3.63e+8 ?>
PHP Echo, print, printf, sprintf function differences and use