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, "bold";
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 to format, which are described in the documentation for SPRINTF ().
Returns the length of the outputted string.
The text is formatted for later 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 format for calling the printf () function is:
printf (" <格式化字符串> ," <参量表> );
%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%o unsigned integer represented in octal
%g automatic 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 break
②/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 x 16 decimal
6. printf (): examples
Example 1:various examples
Copy CodeThe code is as follows:
$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 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
Copy CodeThe code is as follows:
$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 is:
[Monkey]
[Monkey]
[Monkey]
[0000monkey]
[# # # #monkey]
[Many Monke]
Example 3:zero-padded integers
Copy CodeThe code is as follows:
$isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day);
?>
Example 4:formatting currency
Copy CodeThe code is as follows:
$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
Copy CodeThe code is as follows:
$number = 362525200;
Echo sprintf ("%.3e", $number); Outputs 3.63e+8
?>
http://www.bkjia.com/PHPjc/621718.html www.bkjia.com true http://www.bkjia.com/PHPjc/621718.html techarticle 1. Echo Function: Output function, 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, "bbold/b"; 2. Print Letter ...